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
30 #include <linux/bitmap.h>
31 #include <linux/compiler.h>
32 #include <linux/time64.h>
34 #include "../../perf.h"
36 #include "../callchain.h"
40 #include "../thread.h"
42 #include "../machine.h"
43 #include "../db-export.h"
44 #include "../thread-stack.h"
45 #include "../trace-event.h"
46 #include "../call-path.h"
49 #include "thread_map.h"
51 #include "print_binary.h"
53 #include "mem-events.h"
55 #if PY_MAJOR_VERSION < 3
56 #define _PyUnicode_FromString(arg) \
57 PyString_FromString(arg)
58 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
59 PyString_FromStringAndSize((arg1), (arg2))
60 #define _PyBytes_FromStringAndSize(arg1, arg2) \
61 PyString_FromStringAndSize((arg1), (arg2))
62 #define _PyLong_FromLong(arg) \
64 #define _PyLong_AsLong(arg) \
66 #define _PyCapsule_New(arg1, arg2, arg3) \
67 PyCObject_FromVoidPtr((arg1), (arg2))
69 PyMODINIT_FUNC initperf_trace_context(void);
71 #define _PyUnicode_FromString(arg) \
72 PyUnicode_FromString(arg)
73 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
74 PyUnicode_FromStringAndSize((arg1), (arg2))
75 #define _PyBytes_FromStringAndSize(arg1, arg2) \
76 PyBytes_FromStringAndSize((arg1), (arg2))
77 #define _PyLong_FromLong(arg) \
79 #define _PyLong_AsLong(arg) \
81 #define _PyCapsule_New(arg1, arg2, arg3) \
82 PyCapsule_New((arg1), (arg2), (arg3))
84 PyMODINIT_FUNC PyInit_perf_trace_context(void);
87 #define TRACE_EVENT_TYPE_MAX \
88 ((1 << (sizeof(unsigned short) * 8)) - 1)
90 static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
93 #define N_COMMON_FIELDS 7
95 extern struct scripting_context *scripting_context;
97 static char *cur_field_name;
98 static int zero_flag_atom;
100 static PyObject *main_module, *main_dict;
103 struct db_export dbe;
104 PyObject *evsel_handler;
105 PyObject *machine_handler;
106 PyObject *thread_handler;
107 PyObject *comm_handler;
108 PyObject *comm_thread_handler;
109 PyObject *dso_handler;
110 PyObject *symbol_handler;
111 PyObject *branch_type_handler;
112 PyObject *sample_handler;
113 PyObject *call_path_handler;
114 PyObject *call_return_handler;
115 PyObject *synth_handler;
119 static struct tables tables_global;
121 static void handler_call_die(const char *handler_name) __noreturn;
122 static void handler_call_die(const char *handler_name)
125 Py_FatalError("problem in Python trace event handler");
126 // Py_FatalError does not return
127 // but we have to make the compiler happy
132 * Insert val into into the dictionary and decrement the reference counter.
133 * This is necessary for dictionaries since PyDict_SetItemString() does not
134 * steal a reference, as opposed to PyTuple_SetItem().
136 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
138 PyDict_SetItemString(dict, key, val);
142 static PyObject *get_handler(const char *handler_name)
146 handler = PyDict_GetItemString(main_dict, handler_name);
147 if (handler && !PyCallable_Check(handler))
152 static int get_argument_count(PyObject *handler)
157 * The attribute for the code object is func_code in Python 2,
158 * whereas it is __code__ in Python 3.0+.
160 PyObject *code_obj = PyObject_GetAttrString(handler,
162 if (PyErr_Occurred()) {
164 code_obj = PyObject_GetAttrString(handler,
169 PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
172 arg_count = (int) _PyLong_AsLong(arg_count_obj);
173 Py_DECREF(arg_count_obj);
180 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
184 retval = PyObject_CallObject(handler, args);
186 handler_call_die(die_msg);
190 static void try_call_object(const char *handler_name, PyObject *args)
194 handler = get_handler(handler_name);
196 call_object(handler, args, handler_name);
199 static void define_value(enum tep_print_arg_type field_type,
201 const char *field_name,
202 const char *field_value,
203 const char *field_str)
205 const char *handler_name = "define_flag_value";
207 unsigned long long value;
210 if (field_type == TEP_PRINT_SYMBOL)
211 handler_name = "define_symbolic_value";
215 Py_FatalError("couldn't create Python tuple");
217 value = eval_flag(field_value);
219 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
220 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
221 PyTuple_SetItem(t, n++, _PyLong_FromLong(value));
222 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str));
224 try_call_object(handler_name, t);
229 static void define_values(enum tep_print_arg_type field_type,
230 struct tep_print_flag_sym *field,
232 const char *field_name)
234 define_value(field_type, ev_name, field_name, field->value,
238 define_values(field_type, field->next, ev_name, field_name);
241 static void define_field(enum tep_print_arg_type field_type,
243 const char *field_name,
246 const char *handler_name = "define_flag_field";
250 if (field_type == TEP_PRINT_SYMBOL)
251 handler_name = "define_symbolic_field";
253 if (field_type == TEP_PRINT_FLAGS)
258 Py_FatalError("couldn't create Python tuple");
260 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
261 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
262 if (field_type == TEP_PRINT_FLAGS)
263 PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim));
265 try_call_object(handler_name, t);
270 static void define_event_symbols(struct tep_event *event,
272 struct tep_print_arg *args)
277 switch (args->type) {
281 define_value(TEP_PRINT_FLAGS, ev_name, cur_field_name, "0",
285 case TEP_PRINT_FIELD:
286 free(cur_field_name);
287 cur_field_name = strdup(args->field.name);
289 case TEP_PRINT_FLAGS:
290 define_event_symbols(event, ev_name, args->flags.field);
291 define_field(TEP_PRINT_FLAGS, ev_name, cur_field_name,
293 define_values(TEP_PRINT_FLAGS, args->flags.flags, ev_name,
296 case TEP_PRINT_SYMBOL:
297 define_event_symbols(event, ev_name, args->symbol.field);
298 define_field(TEP_PRINT_SYMBOL, ev_name, cur_field_name, NULL);
299 define_values(TEP_PRINT_SYMBOL, args->symbol.symbols, ev_name,
303 case TEP_PRINT_HEX_STR:
304 define_event_symbols(event, ev_name, args->hex.field);
305 define_event_symbols(event, ev_name, args->hex.size);
307 case TEP_PRINT_INT_ARRAY:
308 define_event_symbols(event, ev_name, args->int_array.field);
309 define_event_symbols(event, ev_name, args->int_array.count);
310 define_event_symbols(event, ev_name, args->int_array.el_size);
312 case TEP_PRINT_STRING:
315 define_event_symbols(event, ev_name, args->typecast.item);
318 if (strcmp(args->op.op, ":") == 0)
320 define_event_symbols(event, ev_name, args->op.left);
321 define_event_symbols(event, ev_name, args->op.right);
324 /* gcc warns for these? */
325 case TEP_PRINT_BSTRING:
326 case TEP_PRINT_DYNAMIC_ARRAY:
327 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
329 case TEP_PRINT_BITMASK:
330 /* we should warn... */
335 define_event_symbols(event, ev_name, args->next);
338 static PyObject *get_field_numeric_entry(struct tep_event *event,
339 struct tep_format_field *field, void *data)
341 bool is_array = field->flags & TEP_FIELD_IS_ARRAY;
342 PyObject *obj = NULL, *list = NULL;
343 unsigned long long val;
344 unsigned int item_size, n_items, i;
347 list = PyList_New(field->arraylen);
348 item_size = field->size / field->arraylen;
349 n_items = field->arraylen;
351 item_size = field->size;
355 for (i = 0; i < n_items; i++) {
357 val = read_size(event, data + field->offset + i * item_size,
359 if (field->flags & TEP_FIELD_IS_SIGNED) {
360 if ((long long)val >= LONG_MIN &&
361 (long long)val <= LONG_MAX)
362 obj = _PyLong_FromLong(val);
364 obj = PyLong_FromLongLong(val);
367 obj = _PyLong_FromLong(val);
369 obj = PyLong_FromUnsignedLongLong(val);
372 PyList_SET_ITEM(list, i, obj);
379 static const char *get_dsoname(struct map *map)
381 const char *dsoname = "[unknown]";
383 if (map && map->dso) {
384 if (symbol_conf.show_kernel_path && map->dso->long_name)
385 dsoname = map->dso->long_name;
387 dsoname = map->dso->name;
393 static PyObject *python_process_callchain(struct perf_sample *sample,
394 struct perf_evsel *evsel,
395 struct addr_location *al)
399 pylist = PyList_New(0);
401 Py_FatalError("couldn't create Python list");
403 if (!symbol_conf.use_callchain || !sample->callchain)
406 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
408 scripting_max_stack) != 0) {
409 pr_err("Failed to resolve callchain. Skipping\n");
412 callchain_cursor_commit(&callchain_cursor);
417 struct callchain_cursor_node *node;
418 node = callchain_cursor_current(&callchain_cursor);
422 pyelem = PyDict_New();
424 Py_FatalError("couldn't create Python dictionary");
427 pydict_set_item_string_decref(pyelem, "ip",
428 PyLong_FromUnsignedLongLong(node->ip));
431 PyObject *pysym = PyDict_New();
433 Py_FatalError("couldn't create Python dictionary");
434 pydict_set_item_string_decref(pysym, "start",
435 PyLong_FromUnsignedLongLong(node->sym->start));
436 pydict_set_item_string_decref(pysym, "end",
437 PyLong_FromUnsignedLongLong(node->sym->end));
438 pydict_set_item_string_decref(pysym, "binding",
439 _PyLong_FromLong(node->sym->binding));
440 pydict_set_item_string_decref(pysym, "name",
441 _PyUnicode_FromStringAndSize(node->sym->name,
442 node->sym->namelen));
443 pydict_set_item_string_decref(pyelem, "sym", pysym);
447 const char *dsoname = get_dsoname(node->map);
449 pydict_set_item_string_decref(pyelem, "dso",
450 _PyUnicode_FromString(dsoname));
453 callchain_cursor_advance(&callchain_cursor);
454 PyList_Append(pylist, pyelem);
462 static PyObject *python_process_brstack(struct perf_sample *sample,
463 struct thread *thread)
465 struct branch_stack *br = sample->branch_stack;
469 pylist = PyList_New(0);
471 Py_FatalError("couldn't create Python list");
476 for (i = 0; i < br->nr; i++) {
478 struct addr_location al;
481 pyelem = PyDict_New();
483 Py_FatalError("couldn't create Python dictionary");
485 pydict_set_item_string_decref(pyelem, "from",
486 PyLong_FromUnsignedLongLong(br->entries[i].from));
487 pydict_set_item_string_decref(pyelem, "to",
488 PyLong_FromUnsignedLongLong(br->entries[i].to));
489 pydict_set_item_string_decref(pyelem, "mispred",
490 PyBool_FromLong(br->entries[i].flags.mispred));
491 pydict_set_item_string_decref(pyelem, "predicted",
492 PyBool_FromLong(br->entries[i].flags.predicted));
493 pydict_set_item_string_decref(pyelem, "in_tx",
494 PyBool_FromLong(br->entries[i].flags.in_tx));
495 pydict_set_item_string_decref(pyelem, "abort",
496 PyBool_FromLong(br->entries[i].flags.abort));
497 pydict_set_item_string_decref(pyelem, "cycles",
498 PyLong_FromUnsignedLongLong(br->entries[i].flags.cycles));
500 thread__find_map_fb(thread, sample->cpumode,
501 br->entries[i].from, &al);
502 dsoname = get_dsoname(al.map);
503 pydict_set_item_string_decref(pyelem, "from_dsoname",
504 _PyUnicode_FromString(dsoname));
506 thread__find_map_fb(thread, sample->cpumode,
507 br->entries[i].to, &al);
508 dsoname = get_dsoname(al.map);
509 pydict_set_item_string_decref(pyelem, "to_dsoname",
510 _PyUnicode_FromString(dsoname));
512 PyList_Append(pylist, pyelem);
520 static unsigned long get_offset(struct symbol *sym, struct addr_location *al)
522 unsigned long offset;
524 if (al->addr < sym->end)
525 offset = al->addr - sym->start;
527 offset = al->addr - al->map->start - sym->start;
532 static int get_symoff(struct symbol *sym, struct addr_location *al,
533 bool print_off, char *bf, int size)
535 unsigned long offset;
537 if (!sym || !sym->name[0])
538 return scnprintf(bf, size, "%s", "[unknown]");
541 return scnprintf(bf, size, "%s", sym->name);
543 offset = get_offset(sym, al);
545 return scnprintf(bf, size, "%s+0x%x", sym->name, offset);
548 static int get_br_mspred(struct branch_flags *flags, char *bf, int size)
550 if (!flags->mispred && !flags->predicted)
551 return scnprintf(bf, size, "%s", "-");
554 return scnprintf(bf, size, "%s", "M");
556 return scnprintf(bf, size, "%s", "P");
559 static PyObject *python_process_brstacksym(struct perf_sample *sample,
560 struct thread *thread)
562 struct branch_stack *br = sample->branch_stack;
566 struct addr_location al;
568 pylist = PyList_New(0);
570 Py_FatalError("couldn't create Python list");
575 for (i = 0; i < br->nr; i++) {
578 pyelem = PyDict_New();
580 Py_FatalError("couldn't create Python dictionary");
582 thread__find_symbol_fb(thread, sample->cpumode,
583 br->entries[i].from, &al);
584 get_symoff(al.sym, &al, true, bf, sizeof(bf));
585 pydict_set_item_string_decref(pyelem, "from",
586 _PyUnicode_FromString(bf));
588 thread__find_symbol_fb(thread, sample->cpumode,
589 br->entries[i].to, &al);
590 get_symoff(al.sym, &al, true, bf, sizeof(bf));
591 pydict_set_item_string_decref(pyelem, "to",
592 _PyUnicode_FromString(bf));
594 get_br_mspred(&br->entries[i].flags, bf, sizeof(bf));
595 pydict_set_item_string_decref(pyelem, "pred",
596 _PyUnicode_FromString(bf));
598 if (br->entries[i].flags.in_tx) {
599 pydict_set_item_string_decref(pyelem, "in_tx",
600 _PyUnicode_FromString("X"));
602 pydict_set_item_string_decref(pyelem, "in_tx",
603 _PyUnicode_FromString("-"));
606 if (br->entries[i].flags.abort) {
607 pydict_set_item_string_decref(pyelem, "abort",
608 _PyUnicode_FromString("A"));
610 pydict_set_item_string_decref(pyelem, "abort",
611 _PyUnicode_FromString("-"));
614 PyList_Append(pylist, pyelem);
622 static PyObject *get_sample_value_as_tuple(struct sample_read_value *value)
628 Py_FatalError("couldn't create Python tuple");
629 PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id));
630 PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value));
634 static void set_sample_read_in_dict(PyObject *dict_sample,
635 struct perf_sample *sample,
636 struct perf_evsel *evsel)
638 u64 read_format = evsel->attr.read_format;
642 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
643 pydict_set_item_string_decref(dict_sample, "time_enabled",
644 PyLong_FromUnsignedLongLong(sample->read.time_enabled));
647 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
648 pydict_set_item_string_decref(dict_sample, "time_running",
649 PyLong_FromUnsignedLongLong(sample->read.time_running));
652 if (read_format & PERF_FORMAT_GROUP)
653 values = PyList_New(sample->read.group.nr);
655 values = PyList_New(1);
658 Py_FatalError("couldn't create Python list");
660 if (read_format & PERF_FORMAT_GROUP) {
661 for (i = 0; i < sample->read.group.nr; i++) {
662 PyObject *t = get_sample_value_as_tuple(&sample->read.group.values[i]);
663 PyList_SET_ITEM(values, i, t);
666 PyObject *t = get_sample_value_as_tuple(&sample->read.one);
667 PyList_SET_ITEM(values, 0, t);
669 pydict_set_item_string_decref(dict_sample, "values", values);
672 static void set_sample_datasrc_in_dict(PyObject *dict,
673 struct perf_sample *sample)
675 struct mem_info mi = { .data_src.val = sample->data_src };
678 pydict_set_item_string_decref(dict, "datasrc",
679 PyLong_FromUnsignedLongLong(sample->data_src));
681 perf_script__meminfo_scnprintf(decode, 100, &mi);
683 pydict_set_item_string_decref(dict, "datasrc_decode",
684 _PyUnicode_FromString(decode));
687 static int regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size)
689 unsigned int i = 0, r;
694 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
695 u64 val = regs->regs[i++];
697 printed += scnprintf(bf + printed, size - printed,
698 "%5s:0x%" PRIx64 " ",
699 perf_reg_name(r), val);
705 static void set_regs_in_dict(PyObject *dict,
706 struct perf_sample *sample,
707 struct perf_evsel *evsel)
709 struct perf_event_attr *attr = &evsel->attr;
712 regs_map(&sample->intr_regs, attr->sample_regs_intr, bf, sizeof(bf));
714 pydict_set_item_string_decref(dict, "iregs",
715 _PyUnicode_FromString(bf));
717 regs_map(&sample->user_regs, attr->sample_regs_user, bf, sizeof(bf));
719 pydict_set_item_string_decref(dict, "uregs",
720 _PyUnicode_FromString(bf));
723 static PyObject *get_perf_sample_dict(struct perf_sample *sample,
724 struct perf_evsel *evsel,
725 struct addr_location *al,
728 PyObject *dict, *dict_sample, *brstack, *brstacksym;
732 Py_FatalError("couldn't create Python dictionary");
734 dict_sample = PyDict_New();
736 Py_FatalError("couldn't create Python dictionary");
738 pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel)));
739 pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->attr, sizeof(evsel->attr)));
741 pydict_set_item_string_decref(dict_sample, "pid",
742 _PyLong_FromLong(sample->pid));
743 pydict_set_item_string_decref(dict_sample, "tid",
744 _PyLong_FromLong(sample->tid));
745 pydict_set_item_string_decref(dict_sample, "cpu",
746 _PyLong_FromLong(sample->cpu));
747 pydict_set_item_string_decref(dict_sample, "ip",
748 PyLong_FromUnsignedLongLong(sample->ip));
749 pydict_set_item_string_decref(dict_sample, "time",
750 PyLong_FromUnsignedLongLong(sample->time));
751 pydict_set_item_string_decref(dict_sample, "period",
752 PyLong_FromUnsignedLongLong(sample->period));
753 pydict_set_item_string_decref(dict_sample, "phys_addr",
754 PyLong_FromUnsignedLongLong(sample->phys_addr));
755 pydict_set_item_string_decref(dict_sample, "addr",
756 PyLong_FromUnsignedLongLong(sample->addr));
757 set_sample_read_in_dict(dict_sample, sample, evsel);
758 pydict_set_item_string_decref(dict_sample, "weight",
759 PyLong_FromUnsignedLongLong(sample->weight));
760 pydict_set_item_string_decref(dict_sample, "transaction",
761 PyLong_FromUnsignedLongLong(sample->transaction));
762 set_sample_datasrc_in_dict(dict_sample, sample);
763 pydict_set_item_string_decref(dict, "sample", dict_sample);
765 pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize(
766 (const char *)sample->raw_data, sample->raw_size));
767 pydict_set_item_string_decref(dict, "comm",
768 _PyUnicode_FromString(thread__comm_str(al->thread)));
770 pydict_set_item_string_decref(dict, "dso",
771 _PyUnicode_FromString(al->map->dso->name));
774 pydict_set_item_string_decref(dict, "symbol",
775 _PyUnicode_FromString(al->sym->name));
778 pydict_set_item_string_decref(dict, "callchain", callchain);
780 brstack = python_process_brstack(sample, al->thread);
781 pydict_set_item_string_decref(dict, "brstack", brstack);
783 brstacksym = python_process_brstacksym(sample, al->thread);
784 pydict_set_item_string_decref(dict, "brstacksym", brstacksym);
786 set_regs_in_dict(dict, sample, evsel);
791 static void python_process_tracepoint(struct perf_sample *sample,
792 struct perf_evsel *evsel,
793 struct addr_location *al)
795 struct tep_event *event = evsel->tp_format;
796 PyObject *handler, *context, *t, *obj = NULL, *callchain;
797 PyObject *dict = NULL, *all_entries_dict = NULL;
798 static char handler_name[256];
799 struct tep_format_field *field;
803 int cpu = sample->cpu;
804 void *data = sample->raw_data;
805 unsigned long long nsecs = sample->time;
806 const char *comm = thread__comm_str(al->thread);
807 const char *default_handler_name = "trace_unhandled";
810 snprintf(handler_name, sizeof(handler_name),
811 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
812 Py_FatalError(handler_name);
815 pid = raw_field_value(event, "common_pid", data);
817 sprintf(handler_name, "%s__%s", event->system, event->name);
819 if (!test_and_set_bit(event->id, events_defined))
820 define_event_symbols(event, handler_name, event->print_fmt.args);
822 handler = get_handler(handler_name);
824 handler = get_handler(default_handler_name);
829 Py_FatalError("couldn't create Python dict");
832 t = PyTuple_New(MAX_FIELDS);
834 Py_FatalError("couldn't create Python tuple");
837 s = nsecs / NSEC_PER_SEC;
838 ns = nsecs - s * NSEC_PER_SEC;
840 scripting_context->event_data = data;
841 scripting_context->pevent = evsel->tp_format->tep;
843 context = _PyCapsule_New(scripting_context, NULL, NULL);
845 PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name));
846 PyTuple_SetItem(t, n++, context);
849 callchain = python_process_callchain(sample, evsel, al);
850 /* Need an additional reference for the perf_sample dict */
851 Py_INCREF(callchain);
854 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
855 PyTuple_SetItem(t, n++, _PyLong_FromLong(s));
856 PyTuple_SetItem(t, n++, _PyLong_FromLong(ns));
857 PyTuple_SetItem(t, n++, _PyLong_FromLong(pid));
858 PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm));
859 PyTuple_SetItem(t, n++, callchain);
861 pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu));
862 pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s));
863 pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns));
864 pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid));
865 pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm));
866 pydict_set_item_string_decref(dict, "common_callchain", callchain);
868 for (field = event->format.fields; field; field = field->next) {
869 unsigned int offset, len;
870 unsigned long long val;
872 if (field->flags & TEP_FIELD_IS_ARRAY) {
873 offset = field->offset;
875 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
876 val = tep_read_number(scripting_context->pevent,
882 if (field->flags & TEP_FIELD_IS_STRING &&
883 is_printable_array(data + offset, len)) {
884 obj = _PyUnicode_FromString((char *) data + offset);
886 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
887 field->flags &= ~TEP_FIELD_IS_STRING;
889 } else { /* FIELD_IS_NUMERIC */
890 obj = get_field_numeric_entry(event, field, data);
893 PyTuple_SetItem(t, n++, obj);
895 pydict_set_item_string_decref(dict, field->name, obj);
900 PyTuple_SetItem(t, n++, dict);
902 if (get_argument_count(handler) == (int) n + 1) {
903 all_entries_dict = get_perf_sample_dict(sample, evsel, al,
905 PyTuple_SetItem(t, n++, all_entries_dict);
907 Py_DECREF(callchain);
910 if (_PyTuple_Resize(&t, n) == -1)
911 Py_FatalError("error resizing Python tuple");
914 call_object(handler, t, handler_name);
916 call_object(handler, t, default_handler_name);
921 static PyObject *tuple_new(unsigned int sz)
927 Py_FatalError("couldn't create Python tuple");
931 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
933 #if BITS_PER_LONG == 64
934 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
936 #if BITS_PER_LONG == 32
937 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
941 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
943 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
946 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
948 return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s));
951 static int tuple_set_bytes(PyObject *t, unsigned int pos, void *bytes,
954 return PyTuple_SetItem(t, pos, _PyBytes_FromStringAndSize(bytes, sz));
957 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
959 struct tables *tables = container_of(dbe, struct tables, dbe);
964 tuple_set_u64(t, 0, evsel->db_id);
965 tuple_set_string(t, 1, perf_evsel__name(evsel));
967 call_object(tables->evsel_handler, t, "evsel_table");
974 static int python_export_machine(struct db_export *dbe,
975 struct machine *machine)
977 struct tables *tables = container_of(dbe, struct tables, dbe);
982 tuple_set_u64(t, 0, machine->db_id);
983 tuple_set_s32(t, 1, machine->pid);
984 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
986 call_object(tables->machine_handler, t, "machine_table");
993 static int python_export_thread(struct db_export *dbe, struct thread *thread,
994 u64 main_thread_db_id, struct machine *machine)
996 struct tables *tables = container_of(dbe, struct tables, dbe);
1001 tuple_set_u64(t, 0, thread->db_id);
1002 tuple_set_u64(t, 1, machine->db_id);
1003 tuple_set_u64(t, 2, main_thread_db_id);
1004 tuple_set_s32(t, 3, thread->pid_);
1005 tuple_set_s32(t, 4, thread->tid);
1007 call_object(tables->thread_handler, t, "thread_table");
1014 static int python_export_comm(struct db_export *dbe, struct comm *comm)
1016 struct tables *tables = container_of(dbe, struct tables, dbe);
1021 tuple_set_u64(t, 0, comm->db_id);
1022 tuple_set_string(t, 1, comm__str(comm));
1024 call_object(tables->comm_handler, t, "comm_table");
1031 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
1032 struct comm *comm, struct thread *thread)
1034 struct tables *tables = container_of(dbe, struct tables, dbe);
1039 tuple_set_u64(t, 0, db_id);
1040 tuple_set_u64(t, 1, comm->db_id);
1041 tuple_set_u64(t, 2, thread->db_id);
1043 call_object(tables->comm_thread_handler, t, "comm_thread_table");
1050 static int python_export_dso(struct db_export *dbe, struct dso *dso,
1051 struct machine *machine)
1053 struct tables *tables = container_of(dbe, struct tables, dbe);
1054 char sbuild_id[SBUILD_ID_SIZE];
1057 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1061 tuple_set_u64(t, 0, dso->db_id);
1062 tuple_set_u64(t, 1, machine->db_id);
1063 tuple_set_string(t, 2, dso->short_name);
1064 tuple_set_string(t, 3, dso->long_name);
1065 tuple_set_string(t, 4, sbuild_id);
1067 call_object(tables->dso_handler, t, "dso_table");
1074 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
1077 struct tables *tables = container_of(dbe, struct tables, dbe);
1078 u64 *sym_db_id = symbol__priv(sym);
1083 tuple_set_u64(t, 0, *sym_db_id);
1084 tuple_set_u64(t, 1, dso->db_id);
1085 tuple_set_u64(t, 2, sym->start);
1086 tuple_set_u64(t, 3, sym->end);
1087 tuple_set_s32(t, 4, sym->binding);
1088 tuple_set_string(t, 5, sym->name);
1090 call_object(tables->symbol_handler, t, "symbol_table");
1097 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
1100 struct tables *tables = container_of(dbe, struct tables, dbe);
1105 tuple_set_s32(t, 0, branch_type);
1106 tuple_set_string(t, 1, name);
1108 call_object(tables->branch_type_handler, t, "branch_type_table");
1115 static void python_export_sample_table(struct db_export *dbe,
1116 struct export_sample *es)
1118 struct tables *tables = container_of(dbe, struct tables, dbe);
1123 tuple_set_u64(t, 0, es->db_id);
1124 tuple_set_u64(t, 1, es->evsel->db_id);
1125 tuple_set_u64(t, 2, es->al->machine->db_id);
1126 tuple_set_u64(t, 3, es->al->thread->db_id);
1127 tuple_set_u64(t, 4, es->comm_db_id);
1128 tuple_set_u64(t, 5, es->dso_db_id);
1129 tuple_set_u64(t, 6, es->sym_db_id);
1130 tuple_set_u64(t, 7, es->offset);
1131 tuple_set_u64(t, 8, es->sample->ip);
1132 tuple_set_u64(t, 9, es->sample->time);
1133 tuple_set_s32(t, 10, es->sample->cpu);
1134 tuple_set_u64(t, 11, es->addr_dso_db_id);
1135 tuple_set_u64(t, 12, es->addr_sym_db_id);
1136 tuple_set_u64(t, 13, es->addr_offset);
1137 tuple_set_u64(t, 14, es->sample->addr);
1138 tuple_set_u64(t, 15, es->sample->period);
1139 tuple_set_u64(t, 16, es->sample->weight);
1140 tuple_set_u64(t, 17, es->sample->transaction);
1141 tuple_set_u64(t, 18, es->sample->data_src);
1142 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
1143 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
1144 tuple_set_u64(t, 21, es->call_path_id);
1145 tuple_set_u64(t, 22, es->sample->insn_cnt);
1146 tuple_set_u64(t, 23, es->sample->cyc_cnt);
1148 call_object(tables->sample_handler, t, "sample_table");
1153 static void python_export_synth(struct db_export *dbe, struct export_sample *es)
1155 struct tables *tables = container_of(dbe, struct tables, dbe);
1160 tuple_set_u64(t, 0, es->db_id);
1161 tuple_set_u64(t, 1, es->evsel->attr.config);
1162 tuple_set_bytes(t, 2, es->sample->raw_data, es->sample->raw_size);
1164 call_object(tables->synth_handler, t, "synth_data");
1169 static int python_export_sample(struct db_export *dbe,
1170 struct export_sample *es)
1172 struct tables *tables = container_of(dbe, struct tables, dbe);
1174 python_export_sample_table(dbe, es);
1176 if (es->evsel->attr.type == PERF_TYPE_SYNTH && tables->synth_handler)
1177 python_export_synth(dbe, es);
1182 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
1184 struct tables *tables = container_of(dbe, struct tables, dbe);
1186 u64 parent_db_id, sym_db_id;
1188 parent_db_id = cp->parent ? cp->parent->db_id : 0;
1189 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
1193 tuple_set_u64(t, 0, cp->db_id);
1194 tuple_set_u64(t, 1, parent_db_id);
1195 tuple_set_u64(t, 2, sym_db_id);
1196 tuple_set_u64(t, 3, cp->ip);
1198 call_object(tables->call_path_handler, t, "call_path_table");
1205 static int python_export_call_return(struct db_export *dbe,
1206 struct call_return *cr)
1208 struct tables *tables = container_of(dbe, struct tables, dbe);
1209 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
1214 tuple_set_u64(t, 0, cr->db_id);
1215 tuple_set_u64(t, 1, cr->thread->db_id);
1216 tuple_set_u64(t, 2, comm_db_id);
1217 tuple_set_u64(t, 3, cr->cp->db_id);
1218 tuple_set_u64(t, 4, cr->call_time);
1219 tuple_set_u64(t, 5, cr->return_time);
1220 tuple_set_u64(t, 6, cr->branch_count);
1221 tuple_set_u64(t, 7, cr->call_ref);
1222 tuple_set_u64(t, 8, cr->return_ref);
1223 tuple_set_u64(t, 9, cr->cp->parent->db_id);
1224 tuple_set_s32(t, 10, cr->flags);
1225 tuple_set_u64(t, 11, cr->parent_db_id);
1226 tuple_set_u64(t, 12, cr->insn_count);
1227 tuple_set_u64(t, 13, cr->cyc_count);
1229 call_object(tables->call_return_handler, t, "call_return_table");
1236 static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
1239 struct db_export *dbe = data;
1241 return db_export__call_return(dbe, cr, parent_db_id);
1244 static void python_process_general_event(struct perf_sample *sample,
1245 struct perf_evsel *evsel,
1246 struct addr_location *al)
1248 PyObject *handler, *t, *dict, *callchain;
1249 static char handler_name[64];
1252 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
1254 handler = get_handler(handler_name);
1259 * Use the MAX_FIELDS to make the function expandable, though
1260 * currently there is only one item for the tuple.
1262 t = PyTuple_New(MAX_FIELDS);
1264 Py_FatalError("couldn't create Python tuple");
1267 callchain = python_process_callchain(sample, evsel, al);
1268 dict = get_perf_sample_dict(sample, evsel, al, callchain);
1270 PyTuple_SetItem(t, n++, dict);
1271 if (_PyTuple_Resize(&t, n) == -1)
1272 Py_FatalError("error resizing Python tuple");
1274 call_object(handler, t, handler_name);
1279 static void python_process_event(union perf_event *event,
1280 struct perf_sample *sample,
1281 struct perf_evsel *evsel,
1282 struct addr_location *al)
1284 struct tables *tables = &tables_global;
1286 switch (evsel->attr.type) {
1287 case PERF_TYPE_TRACEPOINT:
1288 python_process_tracepoint(sample, evsel, al);
1290 /* Reserve for future process_hw/sw/raw APIs */
1292 if (tables->db_export_mode)
1293 db_export__sample(&tables->dbe, event, sample, evsel, al);
1295 python_process_general_event(sample, evsel, al);
1299 static void get_handler_name(char *str, size_t size,
1300 struct perf_evsel *evsel)
1304 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
1306 while ((p = strchr(p, ':'))) {
1313 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
1314 struct perf_counts_values *count)
1316 PyObject *handler, *t;
1317 static char handler_name[256];
1320 t = PyTuple_New(MAX_FIELDS);
1322 Py_FatalError("couldn't create Python tuple");
1324 get_handler_name(handler_name, sizeof(handler_name),
1327 handler = get_handler(handler_name);
1329 pr_debug("can't find python handler %s\n", handler_name);
1333 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
1334 PyTuple_SetItem(t, n++, _PyLong_FromLong(thread));
1336 tuple_set_u64(t, n++, tstamp);
1337 tuple_set_u64(t, n++, count->val);
1338 tuple_set_u64(t, n++, count->ena);
1339 tuple_set_u64(t, n++, count->run);
1341 if (_PyTuple_Resize(&t, n) == -1)
1342 Py_FatalError("error resizing Python tuple");
1344 call_object(handler, t, handler_name);
1349 static void python_process_stat(struct perf_stat_config *config,
1350 struct perf_evsel *counter, u64 tstamp)
1352 struct thread_map *threads = counter->threads;
1353 struct cpu_map *cpus = counter->cpus;
1356 if (config->aggr_mode == AGGR_GLOBAL) {
1357 process_stat(counter, -1, -1, tstamp,
1358 &counter->counts->aggr);
1362 for (thread = 0; thread < threads->nr; thread++) {
1363 for (cpu = 0; cpu < cpus->nr; cpu++) {
1364 process_stat(counter, cpus->map[cpu],
1365 thread_map__pid(threads, thread), tstamp,
1366 perf_counts(counter->counts, cpu, thread));
1371 static void python_process_stat_interval(u64 tstamp)
1373 PyObject *handler, *t;
1374 static const char handler_name[] = "stat__interval";
1377 t = PyTuple_New(MAX_FIELDS);
1379 Py_FatalError("couldn't create Python tuple");
1381 handler = get_handler(handler_name);
1383 pr_debug("can't find python handler %s\n", handler_name);
1387 tuple_set_u64(t, n++, tstamp);
1389 if (_PyTuple_Resize(&t, n) == -1)
1390 Py_FatalError("error resizing Python tuple");
1392 call_object(handler, t, handler_name);
1397 static int run_start_sub(void)
1399 main_module = PyImport_AddModule("__main__");
1400 if (main_module == NULL)
1402 Py_INCREF(main_module);
1404 main_dict = PyModule_GetDict(main_module);
1405 if (main_dict == NULL)
1407 Py_INCREF(main_dict);
1409 try_call_object("trace_begin", NULL);
1414 Py_XDECREF(main_dict);
1415 Py_XDECREF(main_module);
1419 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1420 tables->handler_name = get_handler(#table_name); \
1421 if (tables->handler_name) \
1422 tables->dbe.export_ ## name = python_export_ ## name; \
1425 #define SET_TABLE_HANDLER(name) \
1426 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1428 static void set_table_handlers(struct tables *tables)
1430 const char *perf_db_export_mode = "perf_db_export_mode";
1431 const char *perf_db_export_calls = "perf_db_export_calls";
1432 const char *perf_db_export_callchains = "perf_db_export_callchains";
1433 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1434 bool export_calls = false;
1435 bool export_callchains = false;
1438 memset(tables, 0, sizeof(struct tables));
1439 if (db_export__init(&tables->dbe))
1440 Py_FatalError("failed to initialize export");
1442 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1443 if (!db_export_mode)
1446 ret = PyObject_IsTrue(db_export_mode);
1448 handler_call_die(perf_db_export_mode);
1452 /* handle export calls */
1453 tables->dbe.crp = NULL;
1454 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1455 if (db_export_calls) {
1456 ret = PyObject_IsTrue(db_export_calls);
1458 handler_call_die(perf_db_export_calls);
1459 export_calls = !!ret;
1464 call_return_processor__new(python_process_call_return,
1466 if (!tables->dbe.crp)
1467 Py_FatalError("failed to create calls processor");
1470 /* handle export callchains */
1471 tables->dbe.cpr = NULL;
1472 db_export_callchains = PyDict_GetItemString(main_dict,
1473 perf_db_export_callchains);
1474 if (db_export_callchains) {
1475 ret = PyObject_IsTrue(db_export_callchains);
1477 handler_call_die(perf_db_export_callchains);
1478 export_callchains = !!ret;
1481 if (export_callchains) {
1483 * Attempt to use the call path root from the call return
1484 * processor, if the call return processor is in use. Otherwise,
1485 * we allocate a new call path root. This prevents exporting
1486 * duplicate call path ids when both are in use simultaniously.
1488 if (tables->dbe.crp)
1489 tables->dbe.cpr = tables->dbe.crp->cpr;
1491 tables->dbe.cpr = call_path_root__new();
1493 if (!tables->dbe.cpr)
1494 Py_FatalError("failed to create call path root");
1497 tables->db_export_mode = true;
1499 * Reserve per symbol space for symbol->db_id via symbol__priv()
1501 symbol_conf.priv_size = sizeof(u64);
1503 SET_TABLE_HANDLER(evsel);
1504 SET_TABLE_HANDLER(machine);
1505 SET_TABLE_HANDLER(thread);
1506 SET_TABLE_HANDLER(comm);
1507 SET_TABLE_HANDLER(comm_thread);
1508 SET_TABLE_HANDLER(dso);
1509 SET_TABLE_HANDLER(symbol);
1510 SET_TABLE_HANDLER(branch_type);
1511 SET_TABLE_HANDLER(sample);
1512 SET_TABLE_HANDLER(call_path);
1513 SET_TABLE_HANDLER(call_return);
1516 * Synthesized events are samples but with architecture-specific data
1517 * stored in sample->raw_data. They are exported via
1518 * python_export_sample() and consequently do not need a separate export
1521 tables->synth_handler = get_handler("synth_data");
1524 #if PY_MAJOR_VERSION < 3
1525 static void _free_command_line(const char **command_line, int num)
1530 static void _free_command_line(wchar_t **command_line, int num)
1533 for (i = 0; i < num; i++)
1534 PyMem_RawFree(command_line[i]);
1541 * Start trace script
1543 static int python_start_script(const char *script, int argc, const char **argv)
1545 struct tables *tables = &tables_global;
1546 PyMODINIT_FUNC (*initfunc)(void);
1547 #if PY_MAJOR_VERSION < 3
1548 const char **command_line;
1550 wchar_t **command_line;
1553 * Use a non-const name variable to cope with python 2.6's
1554 * PyImport_AppendInittab prototype
1556 char buf[PATH_MAX], name[19] = "perf_trace_context";
1560 #if PY_MAJOR_VERSION < 3
1561 initfunc = initperf_trace_context;
1562 command_line = malloc((argc + 1) * sizeof(const char *));
1563 command_line[0] = script;
1564 for (i = 1; i < argc + 1; i++)
1565 command_line[i] = argv[i - 1];
1567 initfunc = PyInit_perf_trace_context;
1568 command_line = malloc((argc + 1) * sizeof(wchar_t *));
1569 command_line[0] = Py_DecodeLocale(script, NULL);
1570 for (i = 1; i < argc + 1; i++)
1571 command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
1574 PyImport_AppendInittab(name, initfunc);
1577 #if PY_MAJOR_VERSION < 3
1578 PySys_SetArgv(argc + 1, (char **)command_line);
1580 PySys_SetArgv(argc + 1, command_line);
1583 fp = fopen(script, "r");
1585 sprintf(buf, "Can't open python script \"%s\"", script);
1591 err = PyRun_SimpleFile(fp, script);
1593 fprintf(stderr, "Error running python script %s\n", script);
1597 err = run_start_sub();
1599 fprintf(stderr, "Error starting python script %s\n", script);
1603 set_table_handlers(tables);
1605 if (tables->db_export_mode) {
1606 err = db_export__branch_types(&tables->dbe);
1611 _free_command_line(command_line, argc + 1);
1616 _free_command_line(command_line, argc + 1);
1621 static int python_flush_script(void)
1623 struct tables *tables = &tables_global;
1625 return db_export__flush(&tables->dbe);
1631 static int python_stop_script(void)
1633 struct tables *tables = &tables_global;
1635 try_call_object("trace_end", NULL);
1637 db_export__exit(&tables->dbe);
1639 Py_XDECREF(main_dict);
1640 Py_XDECREF(main_module);
1646 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
1648 struct tep_event *event = NULL;
1649 struct tep_format_field *f;
1650 char fname[PATH_MAX];
1651 int not_first, count;
1654 sprintf(fname, "%s.py", outfile);
1655 ofp = fopen(fname, "w");
1657 fprintf(stderr, "couldn't open %s\n", fname);
1660 fprintf(ofp, "# perf script event handlers, "
1661 "generated by perf script -g python\n");
1663 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1664 " License version 2\n\n");
1666 fprintf(ofp, "# The common_* event handler fields are the most useful "
1667 "fields common to\n");
1669 fprintf(ofp, "# all events. They don't necessarily correspond to "
1670 "the 'common_*' fields\n");
1672 fprintf(ofp, "# in the format files. Those fields not available as "
1673 "handler params can\n");
1675 fprintf(ofp, "# be retrieved using Python functions of the form "
1676 "common_*(context).\n");
1678 fprintf(ofp, "# See the perf-script-python Documentation for the list "
1679 "of available functions.\n\n");
1681 fprintf(ofp, "from __future__ import print_function\n\n");
1682 fprintf(ofp, "import os\n");
1683 fprintf(ofp, "import sys\n\n");
1685 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1686 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1687 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1688 fprintf(ofp, "from Core import *\n\n\n");
1690 fprintf(ofp, "def trace_begin():\n");
1691 fprintf(ofp, "\tprint(\"in trace_begin\")\n\n");
1693 fprintf(ofp, "def trace_end():\n");
1694 fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
1696 while ((event = trace_find_next_event(pevent, event))) {
1697 fprintf(ofp, "def %s__%s(", event->system, event->name);
1698 fprintf(ofp, "event_name, ");
1699 fprintf(ofp, "context, ");
1700 fprintf(ofp, "common_cpu,\n");
1701 fprintf(ofp, "\tcommon_secs, ");
1702 fprintf(ofp, "common_nsecs, ");
1703 fprintf(ofp, "common_pid, ");
1704 fprintf(ofp, "common_comm,\n\t");
1705 fprintf(ofp, "common_callchain, ");
1710 for (f = event->format.fields; f; f = f->next) {
1713 if (++count % 5 == 0)
1714 fprintf(ofp, "\n\t");
1716 fprintf(ofp, "%s", f->name);
1720 if (++count % 5 == 0)
1721 fprintf(ofp, "\n\t\t");
1722 fprintf(ofp, "perf_sample_dict");
1724 fprintf(ofp, "):\n");
1726 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1727 "common_secs, common_nsecs,\n\t\t\t"
1728 "common_pid, common_comm)\n\n");
1730 fprintf(ofp, "\t\tprint(\"");
1735 for (f = event->format.fields; f; f = f->next) {
1738 if (count && count % 3 == 0) {
1739 fprintf(ofp, "\" \\\n\t\t\"");
1743 fprintf(ofp, "%s=", f->name);
1744 if (f->flags & TEP_FIELD_IS_STRING ||
1745 f->flags & TEP_FIELD_IS_FLAG ||
1746 f->flags & TEP_FIELD_IS_ARRAY ||
1747 f->flags & TEP_FIELD_IS_SYMBOLIC)
1748 fprintf(ofp, "%%s");
1749 else if (f->flags & TEP_FIELD_IS_SIGNED)
1750 fprintf(ofp, "%%d");
1752 fprintf(ofp, "%%u");
1755 fprintf(ofp, "\" %% \\\n\t\t(");
1760 for (f = event->format.fields; f; f = f->next) {
1764 if (++count % 5 == 0)
1765 fprintf(ofp, "\n\t\t");
1767 if (f->flags & TEP_FIELD_IS_FLAG) {
1768 if ((count - 1) % 5 != 0) {
1769 fprintf(ofp, "\n\t\t");
1772 fprintf(ofp, "flag_str(\"");
1773 fprintf(ofp, "%s__%s\", ", event->system,
1775 fprintf(ofp, "\"%s\", %s)", f->name,
1777 } else if (f->flags & TEP_FIELD_IS_SYMBOLIC) {
1778 if ((count - 1) % 5 != 0) {
1779 fprintf(ofp, "\n\t\t");
1782 fprintf(ofp, "symbol_str(\"");
1783 fprintf(ofp, "%s__%s\", ", event->system,
1785 fprintf(ofp, "\"%s\", %s)", f->name,
1788 fprintf(ofp, "%s", f->name);
1791 fprintf(ofp, "))\n\n");
1793 fprintf(ofp, "\t\tprint('Sample: {'+"
1794 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1796 fprintf(ofp, "\t\tfor node in common_callchain:");
1797 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1798 fprintf(ofp, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))");
1799 fprintf(ofp, "\n\t\t\telse:");
1800 fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n");
1801 fprintf(ofp, "\t\tprint()\n\n");
1805 fprintf(ofp, "def trace_unhandled(event_name, context, "
1806 "event_fields_dict, perf_sample_dict):\n");
1808 fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n");
1809 fprintf(ofp, "\t\tprint('Sample: {'+"
1810 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1812 fprintf(ofp, "def print_header("
1813 "event_name, cpu, secs, nsecs, pid, comm):\n"
1814 "\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1815 "(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n");
1817 fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
1818 "\treturn delimiter.join"
1819 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
1823 fprintf(stderr, "generated Python script: %s\n", fname);
1828 struct scripting_ops python_scripting_ops = {
1830 .start_script = python_start_script,
1831 .flush_script = python_flush_script,
1832 .stop_script = python_stop_script,
1833 .process_event = python_process_event,
1834 .process_stat = python_process_stat,
1835 .process_stat_interval = python_process_stat_interval,
1836 .generate_script = python_generate_script,