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");
59 static void define_value(enum print_arg_type field_type,
61 const char *field_name,
62 const char *field_value,
63 const char *field_str)
65 const char *handler_name = "define_flag_value";
66 PyObject *handler, *t, *retval;
67 unsigned long long value;
70 if (field_type == PRINT_SYMBOL)
71 handler_name = "define_symbolic_value";
75 Py_FatalError("couldn't create Python tuple");
77 value = eval_flag(field_value);
79 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
80 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
81 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
82 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
84 handler = PyDict_GetItemString(main_dict, handler_name);
85 if (handler && PyCallable_Check(handler)) {
86 retval = PyObject_CallObject(handler, t);
88 handler_call_die(handler_name);
94 static void define_values(enum print_arg_type field_type,
95 struct print_flag_sym *field,
97 const char *field_name)
99 define_value(field_type, ev_name, field_name, field->value,
103 define_values(field_type, field->next, ev_name, field_name);
106 static void define_field(enum print_arg_type field_type,
108 const char *field_name,
111 const char *handler_name = "define_flag_field";
112 PyObject *handler, *t, *retval;
115 if (field_type == PRINT_SYMBOL)
116 handler_name = "define_symbolic_field";
118 if (field_type == PRINT_FLAGS)
123 Py_FatalError("couldn't create Python tuple");
125 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
126 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
127 if (field_type == PRINT_FLAGS)
128 PyTuple_SetItem(t, n++, PyString_FromString(delim));
130 handler = PyDict_GetItemString(main_dict, handler_name);
131 if (handler && PyCallable_Check(handler)) {
132 retval = PyObject_CallObject(handler, t);
134 handler_call_die(handler_name);
140 static void define_event_symbols(struct event_format *event,
142 struct print_arg *args)
144 switch (args->type) {
148 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
154 free(cur_field_name);
155 cur_field_name = strdup(args->field.name);
158 define_event_symbols(event, ev_name, args->flags.field);
159 define_field(PRINT_FLAGS, ev_name, cur_field_name,
161 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
165 define_event_symbols(event, ev_name, args->symbol.field);
166 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
167 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
171 define_event_symbols(event, ev_name, args->hex.field);
172 define_event_symbols(event, ev_name, args->hex.size);
177 define_event_symbols(event, ev_name, args->typecast.item);
180 if (strcmp(args->op.op, ":") == 0)
182 define_event_symbols(event, ev_name, args->op.left);
183 define_event_symbols(event, ev_name, args->op.right);
186 /* gcc warns for these? */
188 case PRINT_DYNAMIC_ARRAY:
190 /* we should warn... */
195 define_event_symbols(event, ev_name, args->next);
198 static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
200 static char ev_name[256];
201 struct event_format *event;
202 int type = evsel->attr.config;
205 * XXX: Do we really need to cache this since now we have evsel->tp_format
206 * cached already? Need to re-read this "cache" routine that as well calls
207 * define_event_symbols() :-\
212 events[type] = event = evsel->tp_format;
216 sprintf(ev_name, "%s__%s", event->system, event->name);
218 define_event_symbols(event, ev_name, event->print_fmt.args);
223 static void python_process_tracepoint(union perf_event *perf_event
225 struct perf_sample *sample,
226 struct perf_evsel *evsel,
227 struct machine *machine __maybe_unused,
228 struct addr_location *al)
230 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
231 static char handler_name[256];
232 struct format_field *field;
233 unsigned long long val;
235 struct event_format *event;
238 int cpu = sample->cpu;
239 void *data = sample->raw_data;
240 unsigned long long nsecs = sample->time;
241 struct thread *thread = al->thread;
242 char *comm = thread->comm;
244 t = PyTuple_New(MAX_FIELDS);
246 Py_FatalError("couldn't create Python tuple");
248 event = find_cache_event(evsel);
250 die("ug! no event found for type %d", (int)evsel->attr.config);
252 pid = raw_field_value(event, "common_pid", data);
254 sprintf(handler_name, "%s__%s", event->system, event->name);
256 handler = PyDict_GetItemString(main_dict, handler_name);
257 if (handler && !PyCallable_Check(handler))
262 Py_FatalError("couldn't create Python dict");
264 s = nsecs / NSECS_PER_SEC;
265 ns = nsecs - s * NSECS_PER_SEC;
267 scripting_context->event_data = data;
269 context = PyCObject_FromVoidPtr(scripting_context, NULL);
271 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
272 PyTuple_SetItem(t, n++, context);
275 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
276 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
277 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
278 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
279 PyTuple_SetItem(t, n++, PyString_FromString(comm));
281 PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
282 PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
283 PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
284 PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
285 PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
287 for (field = event->format.fields; field; field = field->next) {
288 if (field->flags & FIELD_IS_STRING) {
290 if (field->flags & FIELD_IS_DYNAMIC) {
291 offset = *(int *)(data + field->offset);
294 offset = field->offset;
295 obj = PyString_FromString((char *)data + offset);
296 } else { /* FIELD_IS_NUMERIC */
297 val = read_size(event, data + field->offset,
299 if (field->flags & FIELD_IS_SIGNED) {
300 if ((long long)val >= LONG_MIN &&
301 (long long)val <= LONG_MAX)
302 obj = PyInt_FromLong(val);
304 obj = PyLong_FromLongLong(val);
307 obj = PyInt_FromLong(val);
309 obj = PyLong_FromUnsignedLongLong(val);
313 PyTuple_SetItem(t, n++, obj);
315 PyDict_SetItemString(dict, field->name, obj);
319 PyTuple_SetItem(t, n++, dict);
321 if (_PyTuple_Resize(&t, n) == -1)
322 Py_FatalError("error resizing Python tuple");
325 retval = PyObject_CallObject(handler, t);
327 handler_call_die(handler_name);
329 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
330 if (handler && PyCallable_Check(handler)) {
332 retval = PyObject_CallObject(handler, t);
334 handler_call_die("trace_unhandled");
342 static void python_process_general_event(union perf_event *perf_event
344 struct perf_sample *sample,
345 struct perf_evsel *evsel,
346 struct machine *machine __maybe_unused,
347 struct addr_location *al)
349 PyObject *handler, *retval, *t, *dict;
350 static char handler_name[64];
352 struct thread *thread = al->thread;
355 * Use the MAX_FIELDS to make the function expandable, though
356 * currently there is only one item for the tuple.
358 t = PyTuple_New(MAX_FIELDS);
360 Py_FatalError("couldn't create Python tuple");
364 Py_FatalError("couldn't create Python dictionary");
366 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
368 handler = PyDict_GetItemString(main_dict, handler_name);
369 if (!handler || !PyCallable_Check(handler))
372 PyDict_SetItemString(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
373 PyDict_SetItemString(dict, "attr", PyString_FromStringAndSize(
374 (const char *)&evsel->attr, sizeof(evsel->attr)));
375 PyDict_SetItemString(dict, "sample", PyString_FromStringAndSize(
376 (const char *)sample, sizeof(*sample)));
377 PyDict_SetItemString(dict, "raw_buf", PyString_FromStringAndSize(
378 (const char *)sample->raw_data, sample->raw_size));
379 PyDict_SetItemString(dict, "comm",
380 PyString_FromString(thread->comm));
382 PyDict_SetItemString(dict, "dso",
383 PyString_FromString(al->map->dso->name));
386 PyDict_SetItemString(dict, "symbol",
387 PyString_FromString(al->sym->name));
390 PyTuple_SetItem(t, n++, dict);
391 if (_PyTuple_Resize(&t, n) == -1)
392 Py_FatalError("error resizing Python tuple");
394 retval = PyObject_CallObject(handler, t);
396 handler_call_die(handler_name);
402 static void python_process_event(union perf_event *perf_event,
403 struct perf_sample *sample,
404 struct perf_evsel *evsel,
405 struct machine *machine,
406 struct addr_location *al)
408 switch (evsel->attr.type) {
409 case PERF_TYPE_TRACEPOINT:
410 python_process_tracepoint(perf_event, sample, evsel,
413 /* Reserve for future process_hw/sw/raw APIs */
415 python_process_general_event(perf_event, sample, evsel,
420 static int run_start_sub(void)
422 PyObject *handler, *retval;
425 main_module = PyImport_AddModule("__main__");
426 if (main_module == NULL)
428 Py_INCREF(main_module);
430 main_dict = PyModule_GetDict(main_module);
431 if (main_dict == NULL) {
435 Py_INCREF(main_dict);
437 handler = PyDict_GetItemString(main_dict, "trace_begin");
438 if (handler == NULL || !PyCallable_Check(handler))
441 retval = PyObject_CallObject(handler, NULL);
443 handler_call_die("trace_begin");
448 Py_XDECREF(main_dict);
449 Py_XDECREF(main_module);
457 static int python_start_script(const char *script, int argc, const char **argv)
459 const char **command_line;
464 command_line = malloc((argc + 1) * sizeof(const char *));
465 command_line[0] = script;
466 for (i = 1; i < argc + 1; i++)
467 command_line[i] = argv[i - 1];
471 initperf_trace_context();
473 PySys_SetArgv(argc + 1, (char **)command_line);
475 fp = fopen(script, "r");
477 sprintf(buf, "Can't open python script \"%s\"", script);
483 err = PyRun_SimpleFile(fp, script);
485 fprintf(stderr, "Error running python script %s\n", script);
489 err = run_start_sub();
491 fprintf(stderr, "Error starting python script %s\n", script);
508 static int python_stop_script(void)
510 PyObject *handler, *retval;
513 handler = PyDict_GetItemString(main_dict, "trace_end");
514 if (handler == NULL || !PyCallable_Check(handler))
517 retval = PyObject_CallObject(handler, NULL);
519 handler_call_die("trace_end");
523 Py_XDECREF(main_dict);
524 Py_XDECREF(main_module);
530 static int python_generate_script(struct pevent *pevent, const char *outfile)
532 struct event_format *event = NULL;
533 struct format_field *f;
534 char fname[PATH_MAX];
535 int not_first, count;
538 sprintf(fname, "%s.py", outfile);
539 ofp = fopen(fname, "w");
541 fprintf(stderr, "couldn't open %s\n", fname);
544 fprintf(ofp, "# perf script event handlers, "
545 "generated by perf script -g python\n");
547 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
548 " License version 2\n\n");
550 fprintf(ofp, "# The common_* event handler fields are the most useful "
551 "fields common to\n");
553 fprintf(ofp, "# all events. They don't necessarily correspond to "
554 "the 'common_*' fields\n");
556 fprintf(ofp, "# in the format files. Those fields not available as "
557 "handler params can\n");
559 fprintf(ofp, "# be retrieved using Python functions of the form "
560 "common_*(context).\n");
562 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
563 "of available functions.\n\n");
565 fprintf(ofp, "import os\n");
566 fprintf(ofp, "import sys\n\n");
568 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
569 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
570 fprintf(ofp, "\nfrom perf_trace_context import *\n");
571 fprintf(ofp, "from Core import *\n\n\n");
573 fprintf(ofp, "def trace_begin():\n");
574 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
576 fprintf(ofp, "def trace_end():\n");
577 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
579 while ((event = trace_find_next_event(pevent, event))) {
580 fprintf(ofp, "def %s__%s(", event->system, event->name);
581 fprintf(ofp, "event_name, ");
582 fprintf(ofp, "context, ");
583 fprintf(ofp, "common_cpu,\n");
584 fprintf(ofp, "\tcommon_secs, ");
585 fprintf(ofp, "common_nsecs, ");
586 fprintf(ofp, "common_pid, ");
587 fprintf(ofp, "common_comm,\n\t");
592 for (f = event->format.fields; f; f = f->next) {
595 if (++count % 5 == 0)
596 fprintf(ofp, "\n\t");
598 fprintf(ofp, "%s", f->name);
600 fprintf(ofp, "):\n");
602 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
603 "common_secs, common_nsecs,\n\t\t\t"
604 "common_pid, common_comm)\n\n");
606 fprintf(ofp, "\t\tprint \"");
611 for (f = event->format.fields; f; f = f->next) {
614 if (count && count % 3 == 0) {
615 fprintf(ofp, "\" \\\n\t\t\"");
619 fprintf(ofp, "%s=", f->name);
620 if (f->flags & FIELD_IS_STRING ||
621 f->flags & FIELD_IS_FLAG ||
622 f->flags & FIELD_IS_SYMBOLIC)
624 else if (f->flags & FIELD_IS_SIGNED)
630 fprintf(ofp, "\\n\" %% \\\n\t\t(");
635 for (f = event->format.fields; f; f = f->next) {
639 if (++count % 5 == 0)
640 fprintf(ofp, "\n\t\t");
642 if (f->flags & FIELD_IS_FLAG) {
643 if ((count - 1) % 5 != 0) {
644 fprintf(ofp, "\n\t\t");
647 fprintf(ofp, "flag_str(\"");
648 fprintf(ofp, "%s__%s\", ", event->system,
650 fprintf(ofp, "\"%s\", %s)", f->name,
652 } else if (f->flags & FIELD_IS_SYMBOLIC) {
653 if ((count - 1) % 5 != 0) {
654 fprintf(ofp, "\n\t\t");
657 fprintf(ofp, "symbol_str(\"");
658 fprintf(ofp, "%s__%s\", ", event->system,
660 fprintf(ofp, "\"%s\", %s)", f->name,
663 fprintf(ofp, "%s", f->name);
666 fprintf(ofp, "),\n\n");
669 fprintf(ofp, "def trace_unhandled(event_name, context, "
670 "event_fields_dict):\n");
672 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
673 "for k,v in sorted(event_fields_dict.items())])\n\n");
675 fprintf(ofp, "def print_header("
676 "event_name, cpu, secs, nsecs, pid, comm):\n"
677 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
678 "(event_name, cpu, secs, nsecs, pid, comm),\n");
682 fprintf(stderr, "generated Python script: %s\n", fname);
687 struct scripting_ops python_scripting_ops = {
689 .start_script = python_start_script,
690 .stop_script = python_stop_script,
691 .process_event = python_process_event,
692 .generate_script = python_generate_script,