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
30 #include "../../perf.h"
32 #include "../callchain.h"
36 #include "../thread.h"
38 #include "../machine.h"
39 #include "../db-export.h"
40 #include "../trace-event.h"
41 #include "../machine.h"
43 PyMODINIT_FUNC initperf_trace_context(void);
45 #define FTRACE_MAX_EVENT \
46 ((1 << (sizeof(unsigned short) * 8)) - 1)
48 struct event_format *events[FTRACE_MAX_EVENT];
51 #define N_COMMON_FIELDS 7
53 extern struct scripting_context *scripting_context;
55 static char *cur_field_name;
56 static int zero_flag_atom;
58 static PyObject *main_module, *main_dict;
62 PyObject *evsel_handler;
63 PyObject *machine_handler;
64 PyObject *thread_handler;
65 PyObject *comm_handler;
66 PyObject *comm_thread_handler;
67 PyObject *dso_handler;
68 PyObject *symbol_handler;
69 PyObject *sample_handler;
73 static struct tables tables_global;
75 static void handler_call_die(const char *handler_name) NORETURN;
76 static void handler_call_die(const char *handler_name)
79 Py_FatalError("problem in Python trace event handler");
80 // Py_FatalError does not return
81 // but we have to make the compiler happy
86 * Insert val into into the dictionary and decrement the reference counter.
87 * This is necessary for dictionaries since PyDict_SetItemString() does not
88 * steal a reference, as opposed to PyTuple_SetItem().
90 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
92 PyDict_SetItemString(dict, key, val);
96 static PyObject *get_handler(const char *handler_name)
100 handler = PyDict_GetItemString(main_dict, handler_name);
101 if (handler && !PyCallable_Check(handler))
106 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
110 retval = PyObject_CallObject(handler, args);
112 handler_call_die(die_msg);
116 static void try_call_object(const char *handler_name, PyObject *args)
120 handler = get_handler(handler_name);
122 call_object(handler, args, handler_name);
125 static void define_value(enum print_arg_type field_type,
127 const char *field_name,
128 const char *field_value,
129 const char *field_str)
131 const char *handler_name = "define_flag_value";
133 unsigned long long value;
136 if (field_type == PRINT_SYMBOL)
137 handler_name = "define_symbolic_value";
141 Py_FatalError("couldn't create Python tuple");
143 value = eval_flag(field_value);
145 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
146 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
147 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
148 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
150 try_call_object(handler_name, t);
155 static void define_values(enum print_arg_type field_type,
156 struct print_flag_sym *field,
158 const char *field_name)
160 define_value(field_type, ev_name, field_name, field->value,
164 define_values(field_type, field->next, ev_name, field_name);
167 static void define_field(enum print_arg_type field_type,
169 const char *field_name,
172 const char *handler_name = "define_flag_field";
176 if (field_type == PRINT_SYMBOL)
177 handler_name = "define_symbolic_field";
179 if (field_type == PRINT_FLAGS)
184 Py_FatalError("couldn't create Python tuple");
186 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
187 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
188 if (field_type == PRINT_FLAGS)
189 PyTuple_SetItem(t, n++, PyString_FromString(delim));
191 try_call_object(handler_name, t);
196 static void define_event_symbols(struct event_format *event,
198 struct print_arg *args)
200 switch (args->type) {
204 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
209 free(cur_field_name);
210 cur_field_name = strdup(args->field.name);
213 define_event_symbols(event, ev_name, args->flags.field);
214 define_field(PRINT_FLAGS, ev_name, cur_field_name,
216 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
220 define_event_symbols(event, ev_name, args->symbol.field);
221 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
222 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
226 define_event_symbols(event, ev_name, args->hex.field);
227 define_event_symbols(event, ev_name, args->hex.size);
232 define_event_symbols(event, ev_name, args->typecast.item);
235 if (strcmp(args->op.op, ":") == 0)
237 define_event_symbols(event, ev_name, args->op.left);
238 define_event_symbols(event, ev_name, args->op.right);
241 /* gcc warns for these? */
243 case PRINT_DYNAMIC_ARRAY:
246 /* we should warn... */
251 define_event_symbols(event, ev_name, args->next);
254 static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
256 static char ev_name[256];
257 struct event_format *event;
258 int type = evsel->attr.config;
261 * XXX: Do we really need to cache this since now we have evsel->tp_format
262 * cached already? Need to re-read this "cache" routine that as well calls
263 * define_event_symbols() :-\
268 events[type] = event = evsel->tp_format;
272 sprintf(ev_name, "%s__%s", event->system, event->name);
274 define_event_symbols(event, ev_name, event->print_fmt.args);
279 static PyObject *get_field_numeric_entry(struct event_format *event,
280 struct format_field *field, void *data)
282 bool is_array = field->flags & FIELD_IS_ARRAY;
283 PyObject *obj, *list = NULL;
284 unsigned long long val;
285 unsigned int item_size, n_items, i;
288 list = PyList_New(field->arraylen);
289 item_size = field->size / field->arraylen;
290 n_items = field->arraylen;
292 item_size = field->size;
296 for (i = 0; i < n_items; i++) {
298 val = read_size(event, data + field->offset + i * item_size,
300 if (field->flags & FIELD_IS_SIGNED) {
301 if ((long long)val >= LONG_MIN &&
302 (long long)val <= LONG_MAX)
303 obj = PyInt_FromLong(val);
305 obj = PyLong_FromLongLong(val);
308 obj = PyInt_FromLong(val);
310 obj = PyLong_FromUnsignedLongLong(val);
313 PyList_SET_ITEM(list, i, obj);
321 static PyObject *python_process_callchain(struct perf_sample *sample,
322 struct perf_evsel *evsel,
323 struct addr_location *al)
327 pylist = PyList_New(0);
329 Py_FatalError("couldn't create Python list");
331 if (!symbol_conf.use_callchain || !sample->callchain)
334 if (thread__resolve_callchain(al->thread, evsel,
336 PERF_MAX_STACK_DEPTH) != 0) {
337 pr_err("Failed to resolve callchain. Skipping\n");
340 callchain_cursor_commit(&callchain_cursor);
345 struct callchain_cursor_node *node;
346 node = callchain_cursor_current(&callchain_cursor);
350 pyelem = PyDict_New();
352 Py_FatalError("couldn't create Python dictionary");
355 pydict_set_item_string_decref(pyelem, "ip",
356 PyLong_FromUnsignedLongLong(node->ip));
359 PyObject *pysym = PyDict_New();
361 Py_FatalError("couldn't create Python dictionary");
362 pydict_set_item_string_decref(pysym, "start",
363 PyLong_FromUnsignedLongLong(node->sym->start));
364 pydict_set_item_string_decref(pysym, "end",
365 PyLong_FromUnsignedLongLong(node->sym->end));
366 pydict_set_item_string_decref(pysym, "binding",
367 PyInt_FromLong(node->sym->binding));
368 pydict_set_item_string_decref(pysym, "name",
369 PyString_FromStringAndSize(node->sym->name,
370 node->sym->namelen));
371 pydict_set_item_string_decref(pyelem, "sym", pysym);
375 struct map *map = node->map;
376 const char *dsoname = "[unknown]";
377 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
378 if (symbol_conf.show_kernel_path && map->dso->long_name)
379 dsoname = map->dso->long_name;
380 else if (map->dso->name)
381 dsoname = map->dso->name;
383 pydict_set_item_string_decref(pyelem, "dso",
384 PyString_FromString(dsoname));
387 callchain_cursor_advance(&callchain_cursor);
388 PyList_Append(pylist, pyelem);
397 static void python_process_tracepoint(struct perf_sample *sample,
398 struct perf_evsel *evsel,
399 struct thread *thread,
400 struct addr_location *al)
402 PyObject *handler, *context, *t, *obj, *callchain;
403 PyObject *dict = NULL;
404 static char handler_name[256];
405 struct format_field *field;
407 struct event_format *event;
410 int cpu = sample->cpu;
411 void *data = sample->raw_data;
412 unsigned long long nsecs = sample->time;
413 const char *comm = thread__comm_str(thread);
415 t = PyTuple_New(MAX_FIELDS);
417 Py_FatalError("couldn't create Python tuple");
419 event = find_cache_event(evsel);
421 die("ug! no event found for type %d", (int)evsel->attr.config);
423 pid = raw_field_value(event, "common_pid", data);
425 sprintf(handler_name, "%s__%s", event->system, event->name);
427 handler = get_handler(handler_name);
431 Py_FatalError("couldn't create Python dict");
433 s = nsecs / NSECS_PER_SEC;
434 ns = nsecs - s * NSECS_PER_SEC;
436 scripting_context->event_data = data;
437 scripting_context->pevent = evsel->tp_format->pevent;
439 context = PyCObject_FromVoidPtr(scripting_context, NULL);
441 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
442 PyTuple_SetItem(t, n++, context);
445 callchain = python_process_callchain(sample, evsel, al);
448 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
449 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
450 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
451 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
452 PyTuple_SetItem(t, n++, PyString_FromString(comm));
453 PyTuple_SetItem(t, n++, callchain);
455 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
456 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
457 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
458 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
459 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
460 pydict_set_item_string_decref(dict, "common_callchain", callchain);
462 for (field = event->format.fields; field; field = field->next) {
463 if (field->flags & FIELD_IS_STRING) {
465 if (field->flags & FIELD_IS_DYNAMIC) {
466 offset = *(int *)(data + field->offset);
469 offset = field->offset;
470 obj = PyString_FromString((char *)data + offset);
471 } else { /* FIELD_IS_NUMERIC */
472 obj = get_field_numeric_entry(event, field, data);
475 PyTuple_SetItem(t, n++, obj);
477 pydict_set_item_string_decref(dict, field->name, obj);
482 PyTuple_SetItem(t, n++, dict);
484 if (_PyTuple_Resize(&t, n) == -1)
485 Py_FatalError("error resizing Python tuple");
488 call_object(handler, t, handler_name);
490 try_call_object("trace_unhandled", t);
497 static PyObject *tuple_new(unsigned int sz)
503 Py_FatalError("couldn't create Python tuple");
507 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
509 #if BITS_PER_LONG == 64
510 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
512 #if BITS_PER_LONG == 32
513 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
517 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
519 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
522 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
524 return PyTuple_SetItem(t, pos, PyString_FromString(s));
527 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
529 struct tables *tables = container_of(dbe, struct tables, dbe);
534 tuple_set_u64(t, 0, evsel->db_id);
535 tuple_set_string(t, 1, perf_evsel__name(evsel));
537 call_object(tables->evsel_handler, t, "evsel_table");
544 static int python_export_machine(struct db_export *dbe,
545 struct machine *machine)
547 struct tables *tables = container_of(dbe, struct tables, dbe);
552 tuple_set_u64(t, 0, machine->db_id);
553 tuple_set_s32(t, 1, machine->pid);
554 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
556 call_object(tables->machine_handler, t, "machine_table");
563 static int python_export_thread(struct db_export *dbe, struct thread *thread,
564 u64 main_thread_db_id, struct machine *machine)
566 struct tables *tables = container_of(dbe, struct tables, dbe);
571 tuple_set_u64(t, 0, thread->db_id);
572 tuple_set_u64(t, 1, machine->db_id);
573 tuple_set_u64(t, 2, main_thread_db_id);
574 tuple_set_s32(t, 3, thread->pid_);
575 tuple_set_s32(t, 4, thread->tid);
577 call_object(tables->thread_handler, t, "thread_table");
584 static int python_export_comm(struct db_export *dbe, struct comm *comm)
586 struct tables *tables = container_of(dbe, struct tables, dbe);
591 tuple_set_u64(t, 0, comm->db_id);
592 tuple_set_string(t, 1, comm__str(comm));
594 call_object(tables->comm_handler, t, "comm_table");
601 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
602 struct comm *comm, struct thread *thread)
604 struct tables *tables = container_of(dbe, struct tables, dbe);
609 tuple_set_u64(t, 0, db_id);
610 tuple_set_u64(t, 1, comm->db_id);
611 tuple_set_u64(t, 2, thread->db_id);
613 call_object(tables->comm_thread_handler, t, "comm_thread_table");
620 static int python_export_dso(struct db_export *dbe, struct dso *dso,
621 struct machine *machine)
623 struct tables *tables = container_of(dbe, struct tables, dbe);
624 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
627 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
631 tuple_set_u64(t, 0, dso->db_id);
632 tuple_set_u64(t, 1, machine->db_id);
633 tuple_set_string(t, 2, dso->short_name);
634 tuple_set_string(t, 3, dso->long_name);
635 tuple_set_string(t, 4, sbuild_id);
637 call_object(tables->dso_handler, t, "dso_table");
644 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
647 struct tables *tables = container_of(dbe, struct tables, dbe);
648 u64 *sym_db_id = symbol__priv(sym);
653 tuple_set_u64(t, 0, *sym_db_id);
654 tuple_set_u64(t, 1, dso->db_id);
655 tuple_set_u64(t, 2, sym->start);
656 tuple_set_u64(t, 3, sym->end);
657 tuple_set_s32(t, 4, sym->binding);
658 tuple_set_string(t, 5, sym->name);
660 call_object(tables->symbol_handler, t, "symbol_table");
667 static int python_export_sample(struct db_export *dbe,
668 struct export_sample *es)
670 struct tables *tables = container_of(dbe, struct tables, dbe);
675 tuple_set_u64(t, 0, es->db_id);
676 tuple_set_u64(t, 1, es->evsel->db_id);
677 tuple_set_u64(t, 2, es->al->machine->db_id);
678 tuple_set_u64(t, 3, es->thread->db_id);
679 tuple_set_u64(t, 4, es->comm_db_id);
680 tuple_set_u64(t, 5, es->dso_db_id);
681 tuple_set_u64(t, 6, es->sym_db_id);
682 tuple_set_u64(t, 7, es->offset);
683 tuple_set_u64(t, 8, es->sample->ip);
684 tuple_set_u64(t, 9, es->sample->time);
685 tuple_set_s32(t, 10, es->sample->cpu);
686 tuple_set_u64(t, 11, es->addr_dso_db_id);
687 tuple_set_u64(t, 12, es->addr_sym_db_id);
688 tuple_set_u64(t, 13, es->addr_offset);
689 tuple_set_u64(t, 14, es->sample->addr);
690 tuple_set_u64(t, 15, es->sample->period);
691 tuple_set_u64(t, 16, es->sample->weight);
692 tuple_set_u64(t, 17, es->sample->transaction);
693 tuple_set_u64(t, 18, es->sample->data_src);
695 call_object(tables->sample_handler, t, "sample_table");
702 static void python_process_general_event(struct perf_sample *sample,
703 struct perf_evsel *evsel,
704 struct thread *thread,
705 struct addr_location *al)
707 PyObject *handler, *t, *dict, *callchain, *dict_sample;
708 static char handler_name[64];
712 * Use the MAX_FIELDS to make the function expandable, though
713 * currently there is only one item for the tuple.
715 t = PyTuple_New(MAX_FIELDS);
717 Py_FatalError("couldn't create Python tuple");
721 Py_FatalError("couldn't create Python dictionary");
723 dict_sample = PyDict_New();
725 Py_FatalError("couldn't create Python dictionary");
727 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
729 handler = get_handler(handler_name);
733 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
734 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
735 (const char *)&evsel->attr, sizeof(evsel->attr)));
737 pydict_set_item_string_decref(dict_sample, "pid",
738 PyInt_FromLong(sample->pid));
739 pydict_set_item_string_decref(dict_sample, "tid",
740 PyInt_FromLong(sample->tid));
741 pydict_set_item_string_decref(dict_sample, "cpu",
742 PyInt_FromLong(sample->cpu));
743 pydict_set_item_string_decref(dict_sample, "ip",
744 PyLong_FromUnsignedLongLong(sample->ip));
745 pydict_set_item_string_decref(dict_sample, "time",
746 PyLong_FromUnsignedLongLong(sample->time));
747 pydict_set_item_string_decref(dict_sample, "period",
748 PyLong_FromUnsignedLongLong(sample->period));
749 pydict_set_item_string_decref(dict, "sample", dict_sample);
751 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
752 (const char *)sample->raw_data, sample->raw_size));
753 pydict_set_item_string_decref(dict, "comm",
754 PyString_FromString(thread__comm_str(thread)));
756 pydict_set_item_string_decref(dict, "dso",
757 PyString_FromString(al->map->dso->name));
760 pydict_set_item_string_decref(dict, "symbol",
761 PyString_FromString(al->sym->name));
765 callchain = python_process_callchain(sample, evsel, al);
766 pydict_set_item_string_decref(dict, "callchain", callchain);
768 PyTuple_SetItem(t, n++, dict);
769 if (_PyTuple_Resize(&t, n) == -1)
770 Py_FatalError("error resizing Python tuple");
772 call_object(handler, t, handler_name);
778 static void python_process_event(union perf_event *event,
779 struct perf_sample *sample,
780 struct perf_evsel *evsel,
781 struct thread *thread,
782 struct addr_location *al)
784 struct tables *tables = &tables_global;
786 switch (evsel->attr.type) {
787 case PERF_TYPE_TRACEPOINT:
788 python_process_tracepoint(sample, evsel, thread, al);
790 /* Reserve for future process_hw/sw/raw APIs */
792 if (tables->db_export_mode)
793 db_export__sample(&tables->dbe, event, sample, evsel,
796 python_process_general_event(sample, evsel, thread, al);
800 static int run_start_sub(void)
802 main_module = PyImport_AddModule("__main__");
803 if (main_module == NULL)
805 Py_INCREF(main_module);
807 main_dict = PyModule_GetDict(main_module);
808 if (main_dict == NULL)
810 Py_INCREF(main_dict);
812 try_call_object("trace_begin", NULL);
817 Py_XDECREF(main_dict);
818 Py_XDECREF(main_module);
822 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
823 tables->handler_name = get_handler(#table_name); \
824 if (tables->handler_name) \
825 tables->dbe.export_ ## name = python_export_ ## name; \
828 #define SET_TABLE_HANDLER(name) \
829 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
831 static void set_table_handlers(struct tables *tables)
833 const char *perf_db_export_mode = "perf_db_export_mode";
834 PyObject *db_export_mode;
837 memset(tables, 0, sizeof(struct tables));
838 if (db_export__init(&tables->dbe))
839 Py_FatalError("failed to initialize export");
841 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
845 ret = PyObject_IsTrue(db_export_mode);
847 handler_call_die(perf_db_export_mode);
851 tables->db_export_mode = true;
853 * Reserve per symbol space for symbol->db_id via symbol__priv()
855 symbol_conf.priv_size = sizeof(u64);
857 SET_TABLE_HANDLER(evsel);
858 SET_TABLE_HANDLER(machine);
859 SET_TABLE_HANDLER(thread);
860 SET_TABLE_HANDLER(comm);
861 SET_TABLE_HANDLER(comm_thread);
862 SET_TABLE_HANDLER(dso);
863 SET_TABLE_HANDLER(symbol);
864 SET_TABLE_HANDLER(sample);
870 static int python_start_script(const char *script, int argc, const char **argv)
872 struct tables *tables = &tables_global;
873 const char **command_line;
878 command_line = malloc((argc + 1) * sizeof(const char *));
879 command_line[0] = script;
880 for (i = 1; i < argc + 1; i++)
881 command_line[i] = argv[i - 1];
885 initperf_trace_context();
887 PySys_SetArgv(argc + 1, (char **)command_line);
889 fp = fopen(script, "r");
891 sprintf(buf, "Can't open python script \"%s\"", script);
897 err = PyRun_SimpleFile(fp, script);
899 fprintf(stderr, "Error running python script %s\n", script);
903 err = run_start_sub();
905 fprintf(stderr, "Error starting python script %s\n", script);
911 set_table_handlers(tables);
921 static int python_flush_script(void)
929 static int python_stop_script(void)
931 struct tables *tables = &tables_global;
933 try_call_object("trace_end", NULL);
935 db_export__exit(&tables->dbe);
937 Py_XDECREF(main_dict);
938 Py_XDECREF(main_module);
944 static int python_generate_script(struct pevent *pevent, const char *outfile)
946 struct event_format *event = NULL;
947 struct format_field *f;
948 char fname[PATH_MAX];
949 int not_first, count;
952 sprintf(fname, "%s.py", outfile);
953 ofp = fopen(fname, "w");
955 fprintf(stderr, "couldn't open %s\n", fname);
958 fprintf(ofp, "# perf script event handlers, "
959 "generated by perf script -g python\n");
961 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
962 " License version 2\n\n");
964 fprintf(ofp, "# The common_* event handler fields are the most useful "
965 "fields common to\n");
967 fprintf(ofp, "# all events. They don't necessarily correspond to "
968 "the 'common_*' fields\n");
970 fprintf(ofp, "# in the format files. Those fields not available as "
971 "handler params can\n");
973 fprintf(ofp, "# be retrieved using Python functions of the form "
974 "common_*(context).\n");
976 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
977 "of available functions.\n\n");
979 fprintf(ofp, "import os\n");
980 fprintf(ofp, "import sys\n\n");
982 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
983 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
984 fprintf(ofp, "\nfrom perf_trace_context import *\n");
985 fprintf(ofp, "from Core import *\n\n\n");
987 fprintf(ofp, "def trace_begin():\n");
988 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
990 fprintf(ofp, "def trace_end():\n");
991 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
993 while ((event = trace_find_next_event(pevent, event))) {
994 fprintf(ofp, "def %s__%s(", event->system, event->name);
995 fprintf(ofp, "event_name, ");
996 fprintf(ofp, "context, ");
997 fprintf(ofp, "common_cpu,\n");
998 fprintf(ofp, "\tcommon_secs, ");
999 fprintf(ofp, "common_nsecs, ");
1000 fprintf(ofp, "common_pid, ");
1001 fprintf(ofp, "common_comm,\n\t");
1002 fprintf(ofp, "common_callchain, ");
1007 for (f = event->format.fields; f; f = f->next) {
1010 if (++count % 5 == 0)
1011 fprintf(ofp, "\n\t");
1013 fprintf(ofp, "%s", f->name);
1015 fprintf(ofp, "):\n");
1017 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1018 "common_secs, common_nsecs,\n\t\t\t"
1019 "common_pid, common_comm)\n\n");
1021 fprintf(ofp, "\t\tprint \"");
1026 for (f = event->format.fields; f; f = f->next) {
1029 if (count && count % 3 == 0) {
1030 fprintf(ofp, "\" \\\n\t\t\"");
1034 fprintf(ofp, "%s=", f->name);
1035 if (f->flags & FIELD_IS_STRING ||
1036 f->flags & FIELD_IS_FLAG ||
1037 f->flags & FIELD_IS_ARRAY ||
1038 f->flags & FIELD_IS_SYMBOLIC)
1039 fprintf(ofp, "%%s");
1040 else if (f->flags & FIELD_IS_SIGNED)
1041 fprintf(ofp, "%%d");
1043 fprintf(ofp, "%%u");
1046 fprintf(ofp, "\" %% \\\n\t\t(");
1051 for (f = event->format.fields; f; f = f->next) {
1055 if (++count % 5 == 0)
1056 fprintf(ofp, "\n\t\t");
1058 if (f->flags & FIELD_IS_FLAG) {
1059 if ((count - 1) % 5 != 0) {
1060 fprintf(ofp, "\n\t\t");
1063 fprintf(ofp, "flag_str(\"");
1064 fprintf(ofp, "%s__%s\", ", event->system,
1066 fprintf(ofp, "\"%s\", %s)", f->name,
1068 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1069 if ((count - 1) % 5 != 0) {
1070 fprintf(ofp, "\n\t\t");
1073 fprintf(ofp, "symbol_str(\"");
1074 fprintf(ofp, "%s__%s\", ", event->system,
1076 fprintf(ofp, "\"%s\", %s)", f->name,
1079 fprintf(ofp, "%s", f->name);
1082 fprintf(ofp, ")\n\n");
1084 fprintf(ofp, "\t\tfor node in common_callchain:");
1085 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1086 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1087 fprintf(ofp, "\n\t\t\telse:");
1088 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1089 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1093 fprintf(ofp, "def trace_unhandled(event_name, context, "
1094 "event_fields_dict):\n");
1096 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1097 "for k,v in sorted(event_fields_dict.items())])\n\n");
1099 fprintf(ofp, "def print_header("
1100 "event_name, cpu, secs, nsecs, pid, comm):\n"
1101 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1102 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1106 fprintf(stderr, "generated Python script: %s\n", fname);
1111 struct scripting_ops python_scripting_ops = {
1113 .start_script = python_start_script,
1114 .flush_script = python_flush_script,
1115 .stop_script = python_stop_script,
1116 .process_event = python_process_event,
1117 .generate_script = python_generate_script,