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 *branch_type_handler;
70 PyObject *sample_handler;
74 static struct tables tables_global;
76 static void handler_call_die(const char *handler_name) NORETURN;
77 static void handler_call_die(const char *handler_name)
80 Py_FatalError("problem in Python trace event handler");
81 // Py_FatalError does not return
82 // but we have to make the compiler happy
87 * Insert val into into the dictionary and decrement the reference counter.
88 * This is necessary for dictionaries since PyDict_SetItemString() does not
89 * steal a reference, as opposed to PyTuple_SetItem().
91 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
93 PyDict_SetItemString(dict, key, val);
97 static PyObject *get_handler(const char *handler_name)
101 handler = PyDict_GetItemString(main_dict, handler_name);
102 if (handler && !PyCallable_Check(handler))
107 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
111 retval = PyObject_CallObject(handler, args);
113 handler_call_die(die_msg);
117 static void try_call_object(const char *handler_name, PyObject *args)
121 handler = get_handler(handler_name);
123 call_object(handler, args, handler_name);
126 static void define_value(enum print_arg_type field_type,
128 const char *field_name,
129 const char *field_value,
130 const char *field_str)
132 const char *handler_name = "define_flag_value";
134 unsigned long long value;
137 if (field_type == PRINT_SYMBOL)
138 handler_name = "define_symbolic_value";
142 Py_FatalError("couldn't create Python tuple");
144 value = eval_flag(field_value);
146 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
147 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
148 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
149 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
151 try_call_object(handler_name, t);
156 static void define_values(enum print_arg_type field_type,
157 struct print_flag_sym *field,
159 const char *field_name)
161 define_value(field_type, ev_name, field_name, field->value,
165 define_values(field_type, field->next, ev_name, field_name);
168 static void define_field(enum print_arg_type field_type,
170 const char *field_name,
173 const char *handler_name = "define_flag_field";
177 if (field_type == PRINT_SYMBOL)
178 handler_name = "define_symbolic_field";
180 if (field_type == PRINT_FLAGS)
185 Py_FatalError("couldn't create Python tuple");
187 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
188 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
189 if (field_type == PRINT_FLAGS)
190 PyTuple_SetItem(t, n++, PyString_FromString(delim));
192 try_call_object(handler_name, t);
197 static void define_event_symbols(struct event_format *event,
199 struct print_arg *args)
201 switch (args->type) {
205 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
210 free(cur_field_name);
211 cur_field_name = strdup(args->field.name);
214 define_event_symbols(event, ev_name, args->flags.field);
215 define_field(PRINT_FLAGS, ev_name, cur_field_name,
217 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
221 define_event_symbols(event, ev_name, args->symbol.field);
222 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
223 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
227 define_event_symbols(event, ev_name, args->hex.field);
228 define_event_symbols(event, ev_name, args->hex.size);
233 define_event_symbols(event, ev_name, args->typecast.item);
236 if (strcmp(args->op.op, ":") == 0)
238 define_event_symbols(event, ev_name, args->op.left);
239 define_event_symbols(event, ev_name, args->op.right);
242 /* gcc warns for these? */
244 case PRINT_DYNAMIC_ARRAY:
247 /* we should warn... */
252 define_event_symbols(event, ev_name, args->next);
255 static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
257 static char ev_name[256];
258 struct event_format *event;
259 int type = evsel->attr.config;
262 * XXX: Do we really need to cache this since now we have evsel->tp_format
263 * cached already? Need to re-read this "cache" routine that as well calls
264 * define_event_symbols() :-\
269 events[type] = event = evsel->tp_format;
273 sprintf(ev_name, "%s__%s", event->system, event->name);
275 define_event_symbols(event, ev_name, event->print_fmt.args);
280 static PyObject *get_field_numeric_entry(struct event_format *event,
281 struct format_field *field, void *data)
283 bool is_array = field->flags & FIELD_IS_ARRAY;
284 PyObject *obj, *list = NULL;
285 unsigned long long val;
286 unsigned int item_size, n_items, i;
289 list = PyList_New(field->arraylen);
290 item_size = field->size / field->arraylen;
291 n_items = field->arraylen;
293 item_size = field->size;
297 for (i = 0; i < n_items; i++) {
299 val = read_size(event, data + field->offset + i * item_size,
301 if (field->flags & FIELD_IS_SIGNED) {
302 if ((long long)val >= LONG_MIN &&
303 (long long)val <= LONG_MAX)
304 obj = PyInt_FromLong(val);
306 obj = PyLong_FromLongLong(val);
309 obj = PyInt_FromLong(val);
311 obj = PyLong_FromUnsignedLongLong(val);
314 PyList_SET_ITEM(list, i, obj);
322 static PyObject *python_process_callchain(struct perf_sample *sample,
323 struct perf_evsel *evsel,
324 struct addr_location *al)
328 pylist = PyList_New(0);
330 Py_FatalError("couldn't create Python list");
332 if (!symbol_conf.use_callchain || !sample->callchain)
335 if (thread__resolve_callchain(al->thread, evsel,
337 PERF_MAX_STACK_DEPTH) != 0) {
338 pr_err("Failed to resolve callchain. Skipping\n");
341 callchain_cursor_commit(&callchain_cursor);
346 struct callchain_cursor_node *node;
347 node = callchain_cursor_current(&callchain_cursor);
351 pyelem = PyDict_New();
353 Py_FatalError("couldn't create Python dictionary");
356 pydict_set_item_string_decref(pyelem, "ip",
357 PyLong_FromUnsignedLongLong(node->ip));
360 PyObject *pysym = PyDict_New();
362 Py_FatalError("couldn't create Python dictionary");
363 pydict_set_item_string_decref(pysym, "start",
364 PyLong_FromUnsignedLongLong(node->sym->start));
365 pydict_set_item_string_decref(pysym, "end",
366 PyLong_FromUnsignedLongLong(node->sym->end));
367 pydict_set_item_string_decref(pysym, "binding",
368 PyInt_FromLong(node->sym->binding));
369 pydict_set_item_string_decref(pysym, "name",
370 PyString_FromStringAndSize(node->sym->name,
371 node->sym->namelen));
372 pydict_set_item_string_decref(pyelem, "sym", pysym);
376 struct map *map = node->map;
377 const char *dsoname = "[unknown]";
378 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
379 if (symbol_conf.show_kernel_path && map->dso->long_name)
380 dsoname = map->dso->long_name;
381 else if (map->dso->name)
382 dsoname = map->dso->name;
384 pydict_set_item_string_decref(pyelem, "dso",
385 PyString_FromString(dsoname));
388 callchain_cursor_advance(&callchain_cursor);
389 PyList_Append(pylist, pyelem);
398 static void python_process_tracepoint(struct perf_sample *sample,
399 struct perf_evsel *evsel,
400 struct thread *thread,
401 struct addr_location *al)
403 PyObject *handler, *context, *t, *obj, *callchain;
404 PyObject *dict = NULL;
405 static char handler_name[256];
406 struct format_field *field;
408 struct event_format *event;
411 int cpu = sample->cpu;
412 void *data = sample->raw_data;
413 unsigned long long nsecs = sample->time;
414 const char *comm = thread__comm_str(thread);
416 t = PyTuple_New(MAX_FIELDS);
418 Py_FatalError("couldn't create Python tuple");
420 event = find_cache_event(evsel);
422 die("ug! no event found for type %d", (int)evsel->attr.config);
424 pid = raw_field_value(event, "common_pid", data);
426 sprintf(handler_name, "%s__%s", event->system, event->name);
428 handler = get_handler(handler_name);
432 Py_FatalError("couldn't create Python dict");
434 s = nsecs / NSECS_PER_SEC;
435 ns = nsecs - s * NSECS_PER_SEC;
437 scripting_context->event_data = data;
438 scripting_context->pevent = evsel->tp_format->pevent;
440 context = PyCObject_FromVoidPtr(scripting_context, NULL);
442 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
443 PyTuple_SetItem(t, n++, context);
446 callchain = python_process_callchain(sample, evsel, al);
449 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
450 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
451 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
452 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
453 PyTuple_SetItem(t, n++, PyString_FromString(comm));
454 PyTuple_SetItem(t, n++, callchain);
456 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
457 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
458 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
459 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
460 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
461 pydict_set_item_string_decref(dict, "common_callchain", callchain);
463 for (field = event->format.fields; field; field = field->next) {
464 if (field->flags & FIELD_IS_STRING) {
466 if (field->flags & FIELD_IS_DYNAMIC) {
467 offset = *(int *)(data + field->offset);
470 offset = field->offset;
471 obj = PyString_FromString((char *)data + offset);
472 } else { /* FIELD_IS_NUMERIC */
473 obj = get_field_numeric_entry(event, field, data);
476 PyTuple_SetItem(t, n++, obj);
478 pydict_set_item_string_decref(dict, field->name, obj);
483 PyTuple_SetItem(t, n++, dict);
485 if (_PyTuple_Resize(&t, n) == -1)
486 Py_FatalError("error resizing Python tuple");
489 call_object(handler, t, handler_name);
491 try_call_object("trace_unhandled", t);
498 static PyObject *tuple_new(unsigned int sz)
504 Py_FatalError("couldn't create Python tuple");
508 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
510 #if BITS_PER_LONG == 64
511 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
513 #if BITS_PER_LONG == 32
514 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
518 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
520 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
523 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
525 return PyTuple_SetItem(t, pos, PyString_FromString(s));
528 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
530 struct tables *tables = container_of(dbe, struct tables, dbe);
535 tuple_set_u64(t, 0, evsel->db_id);
536 tuple_set_string(t, 1, perf_evsel__name(evsel));
538 call_object(tables->evsel_handler, t, "evsel_table");
545 static int python_export_machine(struct db_export *dbe,
546 struct machine *machine)
548 struct tables *tables = container_of(dbe, struct tables, dbe);
553 tuple_set_u64(t, 0, machine->db_id);
554 tuple_set_s32(t, 1, machine->pid);
555 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
557 call_object(tables->machine_handler, t, "machine_table");
564 static int python_export_thread(struct db_export *dbe, struct thread *thread,
565 u64 main_thread_db_id, struct machine *machine)
567 struct tables *tables = container_of(dbe, struct tables, dbe);
572 tuple_set_u64(t, 0, thread->db_id);
573 tuple_set_u64(t, 1, machine->db_id);
574 tuple_set_u64(t, 2, main_thread_db_id);
575 tuple_set_s32(t, 3, thread->pid_);
576 tuple_set_s32(t, 4, thread->tid);
578 call_object(tables->thread_handler, t, "thread_table");
585 static int python_export_comm(struct db_export *dbe, struct comm *comm)
587 struct tables *tables = container_of(dbe, struct tables, dbe);
592 tuple_set_u64(t, 0, comm->db_id);
593 tuple_set_string(t, 1, comm__str(comm));
595 call_object(tables->comm_handler, t, "comm_table");
602 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
603 struct comm *comm, struct thread *thread)
605 struct tables *tables = container_of(dbe, struct tables, dbe);
610 tuple_set_u64(t, 0, db_id);
611 tuple_set_u64(t, 1, comm->db_id);
612 tuple_set_u64(t, 2, thread->db_id);
614 call_object(tables->comm_thread_handler, t, "comm_thread_table");
621 static int python_export_dso(struct db_export *dbe, struct dso *dso,
622 struct machine *machine)
624 struct tables *tables = container_of(dbe, struct tables, dbe);
625 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
628 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
632 tuple_set_u64(t, 0, dso->db_id);
633 tuple_set_u64(t, 1, machine->db_id);
634 tuple_set_string(t, 2, dso->short_name);
635 tuple_set_string(t, 3, dso->long_name);
636 tuple_set_string(t, 4, sbuild_id);
638 call_object(tables->dso_handler, t, "dso_table");
645 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
648 struct tables *tables = container_of(dbe, struct tables, dbe);
649 u64 *sym_db_id = symbol__priv(sym);
654 tuple_set_u64(t, 0, *sym_db_id);
655 tuple_set_u64(t, 1, dso->db_id);
656 tuple_set_u64(t, 2, sym->start);
657 tuple_set_u64(t, 3, sym->end);
658 tuple_set_s32(t, 4, sym->binding);
659 tuple_set_string(t, 5, sym->name);
661 call_object(tables->symbol_handler, t, "symbol_table");
668 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
671 struct tables *tables = container_of(dbe, struct tables, dbe);
676 tuple_set_s32(t, 0, branch_type);
677 tuple_set_string(t, 1, name);
679 call_object(tables->branch_type_handler, t, "branch_type_table");
686 static int python_export_sample(struct db_export *dbe,
687 struct export_sample *es)
689 struct tables *tables = container_of(dbe, struct tables, dbe);
694 tuple_set_u64(t, 0, es->db_id);
695 tuple_set_u64(t, 1, es->evsel->db_id);
696 tuple_set_u64(t, 2, es->al->machine->db_id);
697 tuple_set_u64(t, 3, es->thread->db_id);
698 tuple_set_u64(t, 4, es->comm_db_id);
699 tuple_set_u64(t, 5, es->dso_db_id);
700 tuple_set_u64(t, 6, es->sym_db_id);
701 tuple_set_u64(t, 7, es->offset);
702 tuple_set_u64(t, 8, es->sample->ip);
703 tuple_set_u64(t, 9, es->sample->time);
704 tuple_set_s32(t, 10, es->sample->cpu);
705 tuple_set_u64(t, 11, es->addr_dso_db_id);
706 tuple_set_u64(t, 12, es->addr_sym_db_id);
707 tuple_set_u64(t, 13, es->addr_offset);
708 tuple_set_u64(t, 14, es->sample->addr);
709 tuple_set_u64(t, 15, es->sample->period);
710 tuple_set_u64(t, 16, es->sample->weight);
711 tuple_set_u64(t, 17, es->sample->transaction);
712 tuple_set_u64(t, 18, es->sample->data_src);
713 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
714 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
716 call_object(tables->sample_handler, t, "sample_table");
723 static void python_process_general_event(struct perf_sample *sample,
724 struct perf_evsel *evsel,
725 struct thread *thread,
726 struct addr_location *al)
728 PyObject *handler, *t, *dict, *callchain, *dict_sample;
729 static char handler_name[64];
733 * Use the MAX_FIELDS to make the function expandable, though
734 * currently there is only one item for the tuple.
736 t = PyTuple_New(MAX_FIELDS);
738 Py_FatalError("couldn't create Python tuple");
742 Py_FatalError("couldn't create Python dictionary");
744 dict_sample = PyDict_New();
746 Py_FatalError("couldn't create Python dictionary");
748 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
750 handler = get_handler(handler_name);
754 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
755 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
756 (const char *)&evsel->attr, sizeof(evsel->attr)));
758 pydict_set_item_string_decref(dict_sample, "pid",
759 PyInt_FromLong(sample->pid));
760 pydict_set_item_string_decref(dict_sample, "tid",
761 PyInt_FromLong(sample->tid));
762 pydict_set_item_string_decref(dict_sample, "cpu",
763 PyInt_FromLong(sample->cpu));
764 pydict_set_item_string_decref(dict_sample, "ip",
765 PyLong_FromUnsignedLongLong(sample->ip));
766 pydict_set_item_string_decref(dict_sample, "time",
767 PyLong_FromUnsignedLongLong(sample->time));
768 pydict_set_item_string_decref(dict_sample, "period",
769 PyLong_FromUnsignedLongLong(sample->period));
770 pydict_set_item_string_decref(dict, "sample", dict_sample);
772 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
773 (const char *)sample->raw_data, sample->raw_size));
774 pydict_set_item_string_decref(dict, "comm",
775 PyString_FromString(thread__comm_str(thread)));
777 pydict_set_item_string_decref(dict, "dso",
778 PyString_FromString(al->map->dso->name));
781 pydict_set_item_string_decref(dict, "symbol",
782 PyString_FromString(al->sym->name));
786 callchain = python_process_callchain(sample, evsel, al);
787 pydict_set_item_string_decref(dict, "callchain", callchain);
789 PyTuple_SetItem(t, n++, dict);
790 if (_PyTuple_Resize(&t, n) == -1)
791 Py_FatalError("error resizing Python tuple");
793 call_object(handler, t, handler_name);
799 static void python_process_event(union perf_event *event,
800 struct perf_sample *sample,
801 struct perf_evsel *evsel,
802 struct thread *thread,
803 struct addr_location *al)
805 struct tables *tables = &tables_global;
807 switch (evsel->attr.type) {
808 case PERF_TYPE_TRACEPOINT:
809 python_process_tracepoint(sample, evsel, thread, al);
811 /* Reserve for future process_hw/sw/raw APIs */
813 if (tables->db_export_mode)
814 db_export__sample(&tables->dbe, event, sample, evsel,
817 python_process_general_event(sample, evsel, thread, al);
821 static int run_start_sub(void)
823 main_module = PyImport_AddModule("__main__");
824 if (main_module == NULL)
826 Py_INCREF(main_module);
828 main_dict = PyModule_GetDict(main_module);
829 if (main_dict == NULL)
831 Py_INCREF(main_dict);
833 try_call_object("trace_begin", NULL);
838 Py_XDECREF(main_dict);
839 Py_XDECREF(main_module);
843 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
844 tables->handler_name = get_handler(#table_name); \
845 if (tables->handler_name) \
846 tables->dbe.export_ ## name = python_export_ ## name; \
849 #define SET_TABLE_HANDLER(name) \
850 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
852 static void set_table_handlers(struct tables *tables)
854 const char *perf_db_export_mode = "perf_db_export_mode";
855 PyObject *db_export_mode;
858 memset(tables, 0, sizeof(struct tables));
859 if (db_export__init(&tables->dbe))
860 Py_FatalError("failed to initialize export");
862 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
866 ret = PyObject_IsTrue(db_export_mode);
868 handler_call_die(perf_db_export_mode);
872 tables->db_export_mode = true;
874 * Reserve per symbol space for symbol->db_id via symbol__priv()
876 symbol_conf.priv_size = sizeof(u64);
878 SET_TABLE_HANDLER(evsel);
879 SET_TABLE_HANDLER(machine);
880 SET_TABLE_HANDLER(thread);
881 SET_TABLE_HANDLER(comm);
882 SET_TABLE_HANDLER(comm_thread);
883 SET_TABLE_HANDLER(dso);
884 SET_TABLE_HANDLER(symbol);
885 SET_TABLE_HANDLER(branch_type);
886 SET_TABLE_HANDLER(sample);
892 static int python_start_script(const char *script, int argc, const char **argv)
894 struct tables *tables = &tables_global;
895 const char **command_line;
900 command_line = malloc((argc + 1) * sizeof(const char *));
901 command_line[0] = script;
902 for (i = 1; i < argc + 1; i++)
903 command_line[i] = argv[i - 1];
907 initperf_trace_context();
909 PySys_SetArgv(argc + 1, (char **)command_line);
911 fp = fopen(script, "r");
913 sprintf(buf, "Can't open python script \"%s\"", script);
919 err = PyRun_SimpleFile(fp, script);
921 fprintf(stderr, "Error running python script %s\n", script);
925 err = run_start_sub();
927 fprintf(stderr, "Error starting python script %s\n", script);
933 set_table_handlers(tables);
935 if (tables->db_export_mode) {
936 err = db_export__branch_types(&tables->dbe);
949 static int python_flush_script(void)
957 static int python_stop_script(void)
959 struct tables *tables = &tables_global;
961 try_call_object("trace_end", NULL);
963 db_export__exit(&tables->dbe);
965 Py_XDECREF(main_dict);
966 Py_XDECREF(main_module);
972 static int python_generate_script(struct pevent *pevent, const char *outfile)
974 struct event_format *event = NULL;
975 struct format_field *f;
976 char fname[PATH_MAX];
977 int not_first, count;
980 sprintf(fname, "%s.py", outfile);
981 ofp = fopen(fname, "w");
983 fprintf(stderr, "couldn't open %s\n", fname);
986 fprintf(ofp, "# perf script event handlers, "
987 "generated by perf script -g python\n");
989 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
990 " License version 2\n\n");
992 fprintf(ofp, "# The common_* event handler fields are the most useful "
993 "fields common to\n");
995 fprintf(ofp, "# all events. They don't necessarily correspond to "
996 "the 'common_*' fields\n");
998 fprintf(ofp, "# in the format files. Those fields not available as "
999 "handler params can\n");
1001 fprintf(ofp, "# be retrieved using Python functions of the form "
1002 "common_*(context).\n");
1004 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1005 "of available functions.\n\n");
1007 fprintf(ofp, "import os\n");
1008 fprintf(ofp, "import sys\n\n");
1010 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1011 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1012 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1013 fprintf(ofp, "from Core import *\n\n\n");
1015 fprintf(ofp, "def trace_begin():\n");
1016 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1018 fprintf(ofp, "def trace_end():\n");
1019 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1021 while ((event = trace_find_next_event(pevent, event))) {
1022 fprintf(ofp, "def %s__%s(", event->system, event->name);
1023 fprintf(ofp, "event_name, ");
1024 fprintf(ofp, "context, ");
1025 fprintf(ofp, "common_cpu,\n");
1026 fprintf(ofp, "\tcommon_secs, ");
1027 fprintf(ofp, "common_nsecs, ");
1028 fprintf(ofp, "common_pid, ");
1029 fprintf(ofp, "common_comm,\n\t");
1030 fprintf(ofp, "common_callchain, ");
1035 for (f = event->format.fields; f; f = f->next) {
1038 if (++count % 5 == 0)
1039 fprintf(ofp, "\n\t");
1041 fprintf(ofp, "%s", f->name);
1043 fprintf(ofp, "):\n");
1045 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1046 "common_secs, common_nsecs,\n\t\t\t"
1047 "common_pid, common_comm)\n\n");
1049 fprintf(ofp, "\t\tprint \"");
1054 for (f = event->format.fields; f; f = f->next) {
1057 if (count && count % 3 == 0) {
1058 fprintf(ofp, "\" \\\n\t\t\"");
1062 fprintf(ofp, "%s=", f->name);
1063 if (f->flags & FIELD_IS_STRING ||
1064 f->flags & FIELD_IS_FLAG ||
1065 f->flags & FIELD_IS_ARRAY ||
1066 f->flags & FIELD_IS_SYMBOLIC)
1067 fprintf(ofp, "%%s");
1068 else if (f->flags & FIELD_IS_SIGNED)
1069 fprintf(ofp, "%%d");
1071 fprintf(ofp, "%%u");
1074 fprintf(ofp, "\" %% \\\n\t\t(");
1079 for (f = event->format.fields; f; f = f->next) {
1083 if (++count % 5 == 0)
1084 fprintf(ofp, "\n\t\t");
1086 if (f->flags & FIELD_IS_FLAG) {
1087 if ((count - 1) % 5 != 0) {
1088 fprintf(ofp, "\n\t\t");
1091 fprintf(ofp, "flag_str(\"");
1092 fprintf(ofp, "%s__%s\", ", event->system,
1094 fprintf(ofp, "\"%s\", %s)", f->name,
1096 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1097 if ((count - 1) % 5 != 0) {
1098 fprintf(ofp, "\n\t\t");
1101 fprintf(ofp, "symbol_str(\"");
1102 fprintf(ofp, "%s__%s\", ", event->system,
1104 fprintf(ofp, "\"%s\", %s)", f->name,
1107 fprintf(ofp, "%s", f->name);
1110 fprintf(ofp, ")\n\n");
1112 fprintf(ofp, "\t\tfor node in common_callchain:");
1113 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1114 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1115 fprintf(ofp, "\n\t\t\telse:");
1116 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1117 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1121 fprintf(ofp, "def trace_unhandled(event_name, context, "
1122 "event_fields_dict):\n");
1124 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1125 "for k,v in sorted(event_fields_dict.items())])\n\n");
1127 fprintf(ofp, "def print_header("
1128 "event_name, cpu, secs, nsecs, pid, comm):\n"
1129 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1130 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1134 fprintf(stderr, "generated Python script: %s\n", fname);
1139 struct scripting_ops python_scripting_ops = {
1141 .start_script = python_start_script,
1142 .flush_script = python_flush_script,
1143 .stop_script = python_stop_script,
1144 .process_event = python_process_event,
1145 .generate_script = python_generate_script,