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 <linux/bitmap.h>
31 #include <linux/compiler.h>
32 #include <linux/time64.h>
33 #ifdef HAVE_LIBTRACEEVENT
34 #include <traceevent/event-parse.h>
37 #include "../build-id.h"
38 #include "../counts.h"
41 #include "../callchain.h"
45 #include "../thread.h"
47 #include "../machine.h"
48 #include "../db-export.h"
49 #include "../thread-stack.h"
50 #include "../trace-event.h"
51 #include "../call-path.h"
54 #include "thread_map.h"
55 #include "print_binary.h"
57 #include "mem-events.h"
58 #include "util/perf_regs.h"
60 #if PY_MAJOR_VERSION < 3
61 #define _PyUnicode_FromString(arg) \
62 PyString_FromString(arg)
63 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
64 PyString_FromStringAndSize((arg1), (arg2))
65 #define _PyBytes_FromStringAndSize(arg1, arg2) \
66 PyString_FromStringAndSize((arg1), (arg2))
67 #define _PyLong_FromLong(arg) \
69 #define _PyLong_AsLong(arg) \
71 #define _PyCapsule_New(arg1, arg2, arg3) \
72 PyCObject_FromVoidPtr((arg1), (arg2))
74 PyMODINIT_FUNC initperf_trace_context(void);
76 #define _PyUnicode_FromString(arg) \
77 PyUnicode_FromString(arg)
78 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
79 PyUnicode_FromStringAndSize((arg1), (arg2))
80 #define _PyBytes_FromStringAndSize(arg1, arg2) \
81 PyBytes_FromStringAndSize((arg1), (arg2))
82 #define _PyLong_FromLong(arg) \
84 #define _PyLong_AsLong(arg) \
86 #define _PyCapsule_New(arg1, arg2, arg3) \
87 PyCapsule_New((arg1), (arg2), (arg3))
89 PyMODINIT_FUNC PyInit_perf_trace_context(void);
92 #ifdef HAVE_LIBTRACEEVENT
93 #define TRACE_EVENT_TYPE_MAX \
94 ((1 << (sizeof(unsigned short) * 8)) - 1)
96 #define N_COMMON_FIELDS 7
98 static char *cur_field_name;
99 static int zero_flag_atom;
102 #define MAX_FIELDS 64
104 extern struct scripting_context *scripting_context;
106 static PyObject *main_module, *main_dict;
109 struct db_export dbe;
110 PyObject *evsel_handler;
111 PyObject *machine_handler;
112 PyObject *thread_handler;
113 PyObject *comm_handler;
114 PyObject *comm_thread_handler;
115 PyObject *dso_handler;
116 PyObject *symbol_handler;
117 PyObject *branch_type_handler;
118 PyObject *sample_handler;
119 PyObject *call_path_handler;
120 PyObject *call_return_handler;
121 PyObject *synth_handler;
122 PyObject *context_switch_handler;
126 static struct tables tables_global;
128 static void handler_call_die(const char *handler_name) __noreturn;
129 static void handler_call_die(const char *handler_name)
132 Py_FatalError("problem in Python trace event handler");
133 // Py_FatalError does not return
134 // but we have to make the compiler happy
139 * Insert val into the dictionary and decrement the reference counter.
140 * This is necessary for dictionaries since PyDict_SetItemString() does not
141 * steal a reference, as opposed to PyTuple_SetItem().
143 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
145 PyDict_SetItemString(dict, key, val);
149 static PyObject *get_handler(const char *handler_name)
153 handler = PyDict_GetItemString(main_dict, handler_name);
154 if (handler && !PyCallable_Check(handler))
159 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
163 retval = PyObject_CallObject(handler, args);
165 handler_call_die(die_msg);
169 static void try_call_object(const char *handler_name, PyObject *args)
173 handler = get_handler(handler_name);
175 call_object(handler, args, handler_name);
178 #ifdef HAVE_LIBTRACEEVENT
179 static int get_argument_count(PyObject *handler)
184 * The attribute for the code object is func_code in Python 2,
185 * whereas it is __code__ in Python 3.0+.
187 PyObject *code_obj = PyObject_GetAttrString(handler,
189 if (PyErr_Occurred()) {
191 code_obj = PyObject_GetAttrString(handler,
196 PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
199 arg_count = (int) _PyLong_AsLong(arg_count_obj);
200 Py_DECREF(arg_count_obj);
207 static void define_value(enum tep_print_arg_type field_type,
209 const char *field_name,
210 const char *field_value,
211 const char *field_str)
213 const char *handler_name = "define_flag_value";
215 unsigned long long value;
218 if (field_type == TEP_PRINT_SYMBOL)
219 handler_name = "define_symbolic_value";
223 Py_FatalError("couldn't create Python tuple");
225 value = eval_flag(field_value);
227 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
228 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
229 PyTuple_SetItem(t, n++, _PyLong_FromLong(value));
230 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str));
232 try_call_object(handler_name, t);
237 static void define_values(enum tep_print_arg_type field_type,
238 struct tep_print_flag_sym *field,
240 const char *field_name)
242 define_value(field_type, ev_name, field_name, field->value,
246 define_values(field_type, field->next, ev_name, field_name);
249 static void define_field(enum tep_print_arg_type field_type,
251 const char *field_name,
254 const char *handler_name = "define_flag_field";
258 if (field_type == TEP_PRINT_SYMBOL)
259 handler_name = "define_symbolic_field";
261 if (field_type == TEP_PRINT_FLAGS)
266 Py_FatalError("couldn't create Python tuple");
268 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
269 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
270 if (field_type == TEP_PRINT_FLAGS)
271 PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim));
273 try_call_object(handler_name, t);
278 static void define_event_symbols(struct tep_event *event,
280 struct tep_print_arg *args)
285 switch (args->type) {
289 define_value(TEP_PRINT_FLAGS, ev_name, cur_field_name, "0",
293 case TEP_PRINT_FIELD:
294 free(cur_field_name);
295 cur_field_name = strdup(args->field.name);
297 case TEP_PRINT_FLAGS:
298 define_event_symbols(event, ev_name, args->flags.field);
299 define_field(TEP_PRINT_FLAGS, ev_name, cur_field_name,
301 define_values(TEP_PRINT_FLAGS, args->flags.flags, ev_name,
304 case TEP_PRINT_SYMBOL:
305 define_event_symbols(event, ev_name, args->symbol.field);
306 define_field(TEP_PRINT_SYMBOL, ev_name, cur_field_name, NULL);
307 define_values(TEP_PRINT_SYMBOL, args->symbol.symbols, ev_name,
311 case TEP_PRINT_HEX_STR:
312 define_event_symbols(event, ev_name, args->hex.field);
313 define_event_symbols(event, ev_name, args->hex.size);
315 case TEP_PRINT_INT_ARRAY:
316 define_event_symbols(event, ev_name, args->int_array.field);
317 define_event_symbols(event, ev_name, args->int_array.count);
318 define_event_symbols(event, ev_name, args->int_array.el_size);
320 case TEP_PRINT_STRING:
323 define_event_symbols(event, ev_name, args->typecast.item);
326 if (strcmp(args->op.op, ":") == 0)
328 define_event_symbols(event, ev_name, args->op.left);
329 define_event_symbols(event, ev_name, args->op.right);
332 /* gcc warns for these? */
333 case TEP_PRINT_BSTRING:
334 case TEP_PRINT_DYNAMIC_ARRAY:
335 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
337 case TEP_PRINT_BITMASK:
338 /* we should warn... */
343 define_event_symbols(event, ev_name, args->next);
346 static PyObject *get_field_numeric_entry(struct tep_event *event,
347 struct tep_format_field *field, void *data)
349 bool is_array = field->flags & TEP_FIELD_IS_ARRAY;
350 PyObject *obj = NULL, *list = NULL;
351 unsigned long long val;
352 unsigned int item_size, n_items, i;
355 list = PyList_New(field->arraylen);
356 item_size = field->size / field->arraylen;
357 n_items = field->arraylen;
359 item_size = field->size;
363 for (i = 0; i < n_items; i++) {
365 val = read_size(event, data + field->offset + i * item_size,
367 if (field->flags & TEP_FIELD_IS_SIGNED) {
368 if ((long long)val >= LONG_MIN &&
369 (long long)val <= LONG_MAX)
370 obj = _PyLong_FromLong(val);
372 obj = PyLong_FromLongLong(val);
375 obj = _PyLong_FromLong(val);
377 obj = PyLong_FromUnsignedLongLong(val);
380 PyList_SET_ITEM(list, i, obj);
388 static const char *get_dsoname(struct map *map)
390 const char *dsoname = "[unknown]";
391 struct dso *dso = map ? map__dso(map) : NULL;
394 if (symbol_conf.show_kernel_path && dso->long_name)
395 dsoname = dso->long_name;
403 static unsigned long get_offset(struct symbol *sym, struct addr_location *al)
405 unsigned long offset;
407 if (al->addr < sym->end)
408 offset = al->addr - sym->start;
410 offset = al->addr - map__start(al->map) - sym->start;
415 static PyObject *python_process_callchain(struct perf_sample *sample,
417 struct addr_location *al)
420 struct callchain_cursor *cursor;
422 pylist = PyList_New(0);
424 Py_FatalError("couldn't create Python list");
426 if (!symbol_conf.use_callchain || !sample->callchain)
429 cursor = get_tls_callchain_cursor();
430 if (thread__resolve_callchain(al->thread, cursor, evsel,
432 scripting_max_stack) != 0) {
433 pr_err("Failed to resolve callchain. Skipping\n");
436 callchain_cursor_commit(cursor);
441 struct callchain_cursor_node *node;
442 node = callchain_cursor_current(cursor);
446 pyelem = PyDict_New();
448 Py_FatalError("couldn't create Python dictionary");
451 pydict_set_item_string_decref(pyelem, "ip",
452 PyLong_FromUnsignedLongLong(node->ip));
455 PyObject *pysym = PyDict_New();
457 Py_FatalError("couldn't create Python dictionary");
458 pydict_set_item_string_decref(pysym, "start",
459 PyLong_FromUnsignedLongLong(node->ms.sym->start));
460 pydict_set_item_string_decref(pysym, "end",
461 PyLong_FromUnsignedLongLong(node->ms.sym->end));
462 pydict_set_item_string_decref(pysym, "binding",
463 _PyLong_FromLong(node->ms.sym->binding));
464 pydict_set_item_string_decref(pysym, "name",
465 _PyUnicode_FromStringAndSize(node->ms.sym->name,
466 node->ms.sym->namelen));
467 pydict_set_item_string_decref(pyelem, "sym", pysym);
470 struct map *map = node->ms.map;
471 struct addr_location node_al;
472 unsigned long offset;
474 addr_location__init(&node_al);
475 node_al.addr = map__map_ip(map, node->ip);
476 node_al.map = map__get(map);
477 offset = get_offset(node->ms.sym, &node_al);
478 addr_location__exit(&node_al);
480 pydict_set_item_string_decref(
482 PyLong_FromUnsignedLongLong(offset));
484 if (node->srcline && strcmp(":0", node->srcline)) {
485 pydict_set_item_string_decref(
486 pyelem, "sym_srcline",
487 _PyUnicode_FromString(node->srcline));
492 const char *dsoname = get_dsoname(node->ms.map);
494 pydict_set_item_string_decref(pyelem, "dso",
495 _PyUnicode_FromString(dsoname));
498 callchain_cursor_advance(cursor);
499 PyList_Append(pylist, pyelem);
507 static PyObject *python_process_brstack(struct perf_sample *sample,
508 struct thread *thread)
510 struct branch_stack *br = sample->branch_stack;
511 struct branch_entry *entries = perf_sample__branch_entries(sample);
515 pylist = PyList_New(0);
517 Py_FatalError("couldn't create Python list");
522 for (i = 0; i < br->nr; i++) {
524 struct addr_location al;
527 pyelem = PyDict_New();
529 Py_FatalError("couldn't create Python dictionary");
531 pydict_set_item_string_decref(pyelem, "from",
532 PyLong_FromUnsignedLongLong(entries[i].from));
533 pydict_set_item_string_decref(pyelem, "to",
534 PyLong_FromUnsignedLongLong(entries[i].to));
535 pydict_set_item_string_decref(pyelem, "mispred",
536 PyBool_FromLong(entries[i].flags.mispred));
537 pydict_set_item_string_decref(pyelem, "predicted",
538 PyBool_FromLong(entries[i].flags.predicted));
539 pydict_set_item_string_decref(pyelem, "in_tx",
540 PyBool_FromLong(entries[i].flags.in_tx));
541 pydict_set_item_string_decref(pyelem, "abort",
542 PyBool_FromLong(entries[i].flags.abort));
543 pydict_set_item_string_decref(pyelem, "cycles",
544 PyLong_FromUnsignedLongLong(entries[i].flags.cycles));
546 addr_location__init(&al);
547 thread__find_map_fb(thread, sample->cpumode,
548 entries[i].from, &al);
549 dsoname = get_dsoname(al.map);
550 pydict_set_item_string_decref(pyelem, "from_dsoname",
551 _PyUnicode_FromString(dsoname));
553 thread__find_map_fb(thread, sample->cpumode,
555 dsoname = get_dsoname(al.map);
556 pydict_set_item_string_decref(pyelem, "to_dsoname",
557 _PyUnicode_FromString(dsoname));
559 addr_location__exit(&al);
560 PyList_Append(pylist, pyelem);
568 static int get_symoff(struct symbol *sym, struct addr_location *al,
569 bool print_off, char *bf, int size)
571 unsigned long offset;
573 if (!sym || !sym->name[0])
574 return scnprintf(bf, size, "%s", "[unknown]");
577 return scnprintf(bf, size, "%s", sym->name);
579 offset = get_offset(sym, al);
581 return scnprintf(bf, size, "%s+0x%x", sym->name, offset);
584 static int get_br_mspred(struct branch_flags *flags, char *bf, int size)
586 if (!flags->mispred && !flags->predicted)
587 return scnprintf(bf, size, "%s", "-");
590 return scnprintf(bf, size, "%s", "M");
592 return scnprintf(bf, size, "%s", "P");
595 static PyObject *python_process_brstacksym(struct perf_sample *sample,
596 struct thread *thread)
598 struct branch_stack *br = sample->branch_stack;
599 struct branch_entry *entries = perf_sample__branch_entries(sample);
604 pylist = PyList_New(0);
606 Py_FatalError("couldn't create Python list");
611 for (i = 0; i < br->nr; i++) {
613 struct addr_location al;
615 addr_location__init(&al);
616 pyelem = PyDict_New();
618 Py_FatalError("couldn't create Python dictionary");
620 thread__find_symbol_fb(thread, sample->cpumode,
621 entries[i].from, &al);
622 get_symoff(al.sym, &al, true, bf, sizeof(bf));
623 pydict_set_item_string_decref(pyelem, "from",
624 _PyUnicode_FromString(bf));
626 thread__find_symbol_fb(thread, sample->cpumode,
628 get_symoff(al.sym, &al, true, bf, sizeof(bf));
629 pydict_set_item_string_decref(pyelem, "to",
630 _PyUnicode_FromString(bf));
632 get_br_mspred(&entries[i].flags, bf, sizeof(bf));
633 pydict_set_item_string_decref(pyelem, "pred",
634 _PyUnicode_FromString(bf));
636 if (entries[i].flags.in_tx) {
637 pydict_set_item_string_decref(pyelem, "in_tx",
638 _PyUnicode_FromString("X"));
640 pydict_set_item_string_decref(pyelem, "in_tx",
641 _PyUnicode_FromString("-"));
644 if (entries[i].flags.abort) {
645 pydict_set_item_string_decref(pyelem, "abort",
646 _PyUnicode_FromString("A"));
648 pydict_set_item_string_decref(pyelem, "abort",
649 _PyUnicode_FromString("-"));
652 PyList_Append(pylist, pyelem);
654 addr_location__exit(&al);
661 static PyObject *get_sample_value_as_tuple(struct sample_read_value *value,
668 Py_FatalError("couldn't create Python tuple");
669 PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id));
670 PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value));
671 if (read_format & PERF_FORMAT_LOST)
672 PyTuple_SetItem(t, 2, PyLong_FromUnsignedLongLong(value->lost));
677 static void set_sample_read_in_dict(PyObject *dict_sample,
678 struct perf_sample *sample,
681 u64 read_format = evsel->core.attr.read_format;
685 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
686 pydict_set_item_string_decref(dict_sample, "time_enabled",
687 PyLong_FromUnsignedLongLong(sample->read.time_enabled));
690 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
691 pydict_set_item_string_decref(dict_sample, "time_running",
692 PyLong_FromUnsignedLongLong(sample->read.time_running));
695 if (read_format & PERF_FORMAT_GROUP)
696 values = PyList_New(sample->read.group.nr);
698 values = PyList_New(1);
701 Py_FatalError("couldn't create Python list");
703 if (read_format & PERF_FORMAT_GROUP) {
704 struct sample_read_value *v = sample->read.group.values;
707 sample_read_group__for_each(v, sample->read.group.nr, read_format) {
708 PyObject *t = get_sample_value_as_tuple(v, read_format);
709 PyList_SET_ITEM(values, i, t);
713 PyObject *t = get_sample_value_as_tuple(&sample->read.one,
715 PyList_SET_ITEM(values, 0, t);
717 pydict_set_item_string_decref(dict_sample, "values", values);
720 static void set_sample_datasrc_in_dict(PyObject *dict,
721 struct perf_sample *sample)
723 struct mem_info mi = { .data_src.val = sample->data_src };
726 pydict_set_item_string_decref(dict, "datasrc",
727 PyLong_FromUnsignedLongLong(sample->data_src));
729 perf_script__meminfo_scnprintf(decode, 100, &mi);
731 pydict_set_item_string_decref(dict, "datasrc_decode",
732 _PyUnicode_FromString(decode));
735 static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, char *bf, int size)
737 unsigned int i = 0, r;
745 if (!regs || !regs->regs)
748 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
749 u64 val = regs->regs[i++];
751 printed += scnprintf(bf + printed, size - printed,
752 "%5s:0x%" PRIx64 " ",
753 perf_reg_name(r, arch), val);
757 static void set_regs_in_dict(PyObject *dict,
758 struct perf_sample *sample,
761 struct perf_event_attr *attr = &evsel->core.attr;
762 const char *arch = perf_env__arch(evsel__env(evsel));
765 * Here value 28 is a constant size which can be used to print
766 * one register value and its corresponds to:
767 * 16 chars is to specify 64 bit register in hexadecimal.
768 * 2 chars is for appending "0x" to the hexadecimal value and
769 * 10 chars is for register name.
771 int size = __sw_hweight64(attr->sample_regs_intr) * 28;
772 char *bf = malloc(size);
774 regs_map(&sample->intr_regs, attr->sample_regs_intr, arch, bf, size);
776 pydict_set_item_string_decref(dict, "iregs",
777 _PyUnicode_FromString(bf));
779 regs_map(&sample->user_regs, attr->sample_regs_user, arch, bf, size);
781 pydict_set_item_string_decref(dict, "uregs",
782 _PyUnicode_FromString(bf));
786 static void set_sym_in_dict(PyObject *dict, struct addr_location *al,
787 const char *dso_field, const char *dso_bid_field,
788 const char *dso_map_start, const char *dso_map_end,
789 const char *sym_field, const char *symoff_field)
791 char sbuild_id[SBUILD_ID_SIZE];
794 struct dso *dso = map__dso(al->map);
796 pydict_set_item_string_decref(dict, dso_field, _PyUnicode_FromString(dso->name));
797 build_id__sprintf(&dso->bid, sbuild_id);
798 pydict_set_item_string_decref(dict, dso_bid_field,
799 _PyUnicode_FromString(sbuild_id));
800 pydict_set_item_string_decref(dict, dso_map_start,
801 PyLong_FromUnsignedLong(map__start(al->map)));
802 pydict_set_item_string_decref(dict, dso_map_end,
803 PyLong_FromUnsignedLong(map__end(al->map)));
806 pydict_set_item_string_decref(dict, sym_field,
807 _PyUnicode_FromString(al->sym->name));
808 pydict_set_item_string_decref(dict, symoff_field,
809 PyLong_FromUnsignedLong(get_offset(al->sym, al)));
813 static void set_sample_flags(PyObject *dict, u32 flags)
815 const char *ch = PERF_IP_FLAG_CHARS;
818 for (p = str; *ch; ch++, flags >>= 1) {
823 pydict_set_item_string_decref(dict, "flags", _PyUnicode_FromString(str));
826 static void python_process_sample_flags(struct perf_sample *sample, PyObject *dict_sample)
828 char flags_disp[SAMPLE_FLAGS_BUF_SIZE];
830 set_sample_flags(dict_sample, sample->flags);
831 perf_sample__sprintf_flags(sample->flags, flags_disp, sizeof(flags_disp));
832 pydict_set_item_string_decref(dict_sample, "flags_disp",
833 _PyUnicode_FromString(flags_disp));
836 static PyObject *get_perf_sample_dict(struct perf_sample *sample,
838 struct addr_location *al,
839 struct addr_location *addr_al,
842 PyObject *dict, *dict_sample, *brstack, *brstacksym;
846 Py_FatalError("couldn't create Python dictionary");
848 dict_sample = PyDict_New();
850 Py_FatalError("couldn't create Python dictionary");
852 pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(evsel__name(evsel)));
853 pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->core.attr, sizeof(evsel->core.attr)));
855 pydict_set_item_string_decref(dict_sample, "pid",
856 _PyLong_FromLong(sample->pid));
857 pydict_set_item_string_decref(dict_sample, "tid",
858 _PyLong_FromLong(sample->tid));
859 pydict_set_item_string_decref(dict_sample, "cpu",
860 _PyLong_FromLong(sample->cpu));
861 pydict_set_item_string_decref(dict_sample, "ip",
862 PyLong_FromUnsignedLongLong(sample->ip));
863 pydict_set_item_string_decref(dict_sample, "time",
864 PyLong_FromUnsignedLongLong(sample->time));
865 pydict_set_item_string_decref(dict_sample, "period",
866 PyLong_FromUnsignedLongLong(sample->period));
867 pydict_set_item_string_decref(dict_sample, "phys_addr",
868 PyLong_FromUnsignedLongLong(sample->phys_addr));
869 pydict_set_item_string_decref(dict_sample, "addr",
870 PyLong_FromUnsignedLongLong(sample->addr));
871 set_sample_read_in_dict(dict_sample, sample, evsel);
872 pydict_set_item_string_decref(dict_sample, "weight",
873 PyLong_FromUnsignedLongLong(sample->weight));
874 pydict_set_item_string_decref(dict_sample, "transaction",
875 PyLong_FromUnsignedLongLong(sample->transaction));
876 set_sample_datasrc_in_dict(dict_sample, sample);
877 pydict_set_item_string_decref(dict, "sample", dict_sample);
879 pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize(
880 (const char *)sample->raw_data, sample->raw_size));
881 pydict_set_item_string_decref(dict, "comm",
882 _PyUnicode_FromString(thread__comm_str(al->thread)));
883 set_sym_in_dict(dict, al, "dso", "dso_bid", "dso_map_start", "dso_map_end",
886 pydict_set_item_string_decref(dict, "callchain", callchain);
888 brstack = python_process_brstack(sample, al->thread);
889 pydict_set_item_string_decref(dict, "brstack", brstack);
891 brstacksym = python_process_brstacksym(sample, al->thread);
892 pydict_set_item_string_decref(dict, "brstacksym", brstacksym);
894 if (sample->machine_pid) {
895 pydict_set_item_string_decref(dict_sample, "machine_pid",
896 _PyLong_FromLong(sample->machine_pid));
897 pydict_set_item_string_decref(dict_sample, "vcpu",
898 _PyLong_FromLong(sample->vcpu));
901 pydict_set_item_string_decref(dict_sample, "cpumode",
902 _PyLong_FromLong((unsigned long)sample->cpumode));
905 pydict_set_item_string_decref(dict_sample, "addr_correlates_sym",
907 set_sym_in_dict(dict_sample, addr_al, "addr_dso", "addr_dso_bid",
908 "addr_dso_map_start", "addr_dso_map_end",
909 "addr_symbol", "addr_symoff");
913 python_process_sample_flags(sample, dict_sample);
915 /* Instructions per cycle (IPC) */
916 if (sample->insn_cnt && sample->cyc_cnt) {
917 pydict_set_item_string_decref(dict_sample, "insn_cnt",
918 PyLong_FromUnsignedLongLong(sample->insn_cnt));
919 pydict_set_item_string_decref(dict_sample, "cyc_cnt",
920 PyLong_FromUnsignedLongLong(sample->cyc_cnt));
923 set_regs_in_dict(dict, sample, evsel);
928 #ifdef HAVE_LIBTRACEEVENT
929 static void python_process_tracepoint(struct perf_sample *sample,
931 struct addr_location *al,
932 struct addr_location *addr_al)
934 struct tep_event *event = evsel->tp_format;
935 PyObject *handler, *context, *t, *obj = NULL, *callchain;
936 PyObject *dict = NULL, *all_entries_dict = NULL;
937 static char handler_name[256];
938 struct tep_format_field *field;
942 int cpu = sample->cpu;
943 void *data = sample->raw_data;
944 unsigned long long nsecs = sample->time;
945 const char *comm = thread__comm_str(al->thread);
946 const char *default_handler_name = "trace_unhandled";
947 DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
949 bitmap_zero(events_defined, TRACE_EVENT_TYPE_MAX);
952 snprintf(handler_name, sizeof(handler_name),
953 "ug! no event found for type %" PRIu64, (u64)evsel->core.attr.config);
954 Py_FatalError(handler_name);
957 pid = raw_field_value(event, "common_pid", data);
959 sprintf(handler_name, "%s__%s", event->system, event->name);
961 if (!__test_and_set_bit(event->id, events_defined))
962 define_event_symbols(event, handler_name, event->print_fmt.args);
964 handler = get_handler(handler_name);
966 handler = get_handler(default_handler_name);
971 Py_FatalError("couldn't create Python dict");
974 t = PyTuple_New(MAX_FIELDS);
976 Py_FatalError("couldn't create Python tuple");
979 s = nsecs / NSEC_PER_SEC;
980 ns = nsecs - s * NSEC_PER_SEC;
982 context = _PyCapsule_New(scripting_context, NULL, NULL);
984 PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name));
985 PyTuple_SetItem(t, n++, context);
988 callchain = python_process_callchain(sample, evsel, al);
989 /* Need an additional reference for the perf_sample dict */
990 Py_INCREF(callchain);
993 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
994 PyTuple_SetItem(t, n++, _PyLong_FromLong(s));
995 PyTuple_SetItem(t, n++, _PyLong_FromLong(ns));
996 PyTuple_SetItem(t, n++, _PyLong_FromLong(pid));
997 PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm));
998 PyTuple_SetItem(t, n++, callchain);
1000 pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu));
1001 pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s));
1002 pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns));
1003 pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid));
1004 pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm));
1005 pydict_set_item_string_decref(dict, "common_callchain", callchain);
1007 for (field = event->format.fields; field; field = field->next) {
1008 unsigned int offset, len;
1009 unsigned long long val;
1011 if (field->flags & TEP_FIELD_IS_ARRAY) {
1012 offset = field->offset;
1014 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
1015 val = tep_read_number(scripting_context->pevent,
1016 data + offset, len);
1020 if (tep_field_is_relative(field->flags))
1021 offset += field->offset + field->size;
1023 if (field->flags & TEP_FIELD_IS_STRING &&
1024 is_printable_array(data + offset, len)) {
1025 obj = _PyUnicode_FromString((char *) data + offset);
1027 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
1028 field->flags &= ~TEP_FIELD_IS_STRING;
1030 } else { /* FIELD_IS_NUMERIC */
1031 obj = get_field_numeric_entry(event, field, data);
1034 PyTuple_SetItem(t, n++, obj);
1036 pydict_set_item_string_decref(dict, field->name, obj);
1041 PyTuple_SetItem(t, n++, dict);
1043 if (get_argument_count(handler) == (int) n + 1) {
1044 all_entries_dict = get_perf_sample_dict(sample, evsel, al, addr_al,
1046 PyTuple_SetItem(t, n++, all_entries_dict);
1048 Py_DECREF(callchain);
1051 if (_PyTuple_Resize(&t, n) == -1)
1052 Py_FatalError("error resizing Python tuple");
1055 call_object(handler, t, handler_name);
1057 call_object(handler, t, default_handler_name);
1062 static void python_process_tracepoint(struct perf_sample *sample __maybe_unused,
1063 struct evsel *evsel __maybe_unused,
1064 struct addr_location *al __maybe_unused,
1065 struct addr_location *addr_al __maybe_unused)
1067 fprintf(stderr, "Tracepoint events are not supported because "
1068 "perf is not linked with libtraceevent.\n");
1072 static PyObject *tuple_new(unsigned int sz)
1076 t = PyTuple_New(sz);
1078 Py_FatalError("couldn't create Python tuple");
1082 static int tuple_set_s64(PyObject *t, unsigned int pos, s64 val)
1084 #if BITS_PER_LONG == 64
1085 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
1087 #if BITS_PER_LONG == 32
1088 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
1093 * Databases support only signed 64-bit numbers, so even though we are
1094 * exporting a u64, it must be as s64.
1096 #define tuple_set_d64 tuple_set_s64
1098 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
1100 #if BITS_PER_LONG == 64
1101 return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLong(val));
1103 #if BITS_PER_LONG == 32
1104 return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLongLong(val));
1108 static int tuple_set_u32(PyObject *t, unsigned int pos, u32 val)
1110 return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLong(val));
1113 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
1115 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
1118 static int tuple_set_bool(PyObject *t, unsigned int pos, bool val)
1120 return PyTuple_SetItem(t, pos, PyBool_FromLong(val));
1123 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
1125 return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s));
1128 static int tuple_set_bytes(PyObject *t, unsigned int pos, void *bytes,
1131 return PyTuple_SetItem(t, pos, _PyBytes_FromStringAndSize(bytes, sz));
1134 static int python_export_evsel(struct db_export *dbe, struct evsel *evsel)
1136 struct tables *tables = container_of(dbe, struct tables, dbe);
1141 tuple_set_d64(t, 0, evsel->db_id);
1142 tuple_set_string(t, 1, evsel__name(evsel));
1144 call_object(tables->evsel_handler, t, "evsel_table");
1151 static int python_export_machine(struct db_export *dbe,
1152 struct machine *machine)
1154 struct tables *tables = container_of(dbe, struct tables, dbe);
1159 tuple_set_d64(t, 0, machine->db_id);
1160 tuple_set_s32(t, 1, machine->pid);
1161 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
1163 call_object(tables->machine_handler, t, "machine_table");
1170 static int python_export_thread(struct db_export *dbe, struct thread *thread,
1171 u64 main_thread_db_id, struct machine *machine)
1173 struct tables *tables = container_of(dbe, struct tables, dbe);
1178 tuple_set_d64(t, 0, thread__db_id(thread));
1179 tuple_set_d64(t, 1, machine->db_id);
1180 tuple_set_d64(t, 2, main_thread_db_id);
1181 tuple_set_s32(t, 3, thread__pid(thread));
1182 tuple_set_s32(t, 4, thread__tid(thread));
1184 call_object(tables->thread_handler, t, "thread_table");
1191 static int python_export_comm(struct db_export *dbe, struct comm *comm,
1192 struct thread *thread)
1194 struct tables *tables = container_of(dbe, struct tables, dbe);
1199 tuple_set_d64(t, 0, comm->db_id);
1200 tuple_set_string(t, 1, comm__str(comm));
1201 tuple_set_d64(t, 2, thread__db_id(thread));
1202 tuple_set_d64(t, 3, comm->start);
1203 tuple_set_s32(t, 4, comm->exec);
1205 call_object(tables->comm_handler, t, "comm_table");
1212 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
1213 struct comm *comm, struct thread *thread)
1215 struct tables *tables = container_of(dbe, struct tables, dbe);
1220 tuple_set_d64(t, 0, db_id);
1221 tuple_set_d64(t, 1, comm->db_id);
1222 tuple_set_d64(t, 2, thread__db_id(thread));
1224 call_object(tables->comm_thread_handler, t, "comm_thread_table");
1231 static int python_export_dso(struct db_export *dbe, struct dso *dso,
1232 struct machine *machine)
1234 struct tables *tables = container_of(dbe, struct tables, dbe);
1235 char sbuild_id[SBUILD_ID_SIZE];
1238 build_id__sprintf(&dso->bid, sbuild_id);
1242 tuple_set_d64(t, 0, dso->db_id);
1243 tuple_set_d64(t, 1, machine->db_id);
1244 tuple_set_string(t, 2, dso->short_name);
1245 tuple_set_string(t, 3, dso->long_name);
1246 tuple_set_string(t, 4, sbuild_id);
1248 call_object(tables->dso_handler, t, "dso_table");
1255 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
1258 struct tables *tables = container_of(dbe, struct tables, dbe);
1259 u64 *sym_db_id = symbol__priv(sym);
1264 tuple_set_d64(t, 0, *sym_db_id);
1265 tuple_set_d64(t, 1, dso->db_id);
1266 tuple_set_d64(t, 2, sym->start);
1267 tuple_set_d64(t, 3, sym->end);
1268 tuple_set_s32(t, 4, sym->binding);
1269 tuple_set_string(t, 5, sym->name);
1271 call_object(tables->symbol_handler, t, "symbol_table");
1278 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
1281 struct tables *tables = container_of(dbe, struct tables, dbe);
1286 tuple_set_s32(t, 0, branch_type);
1287 tuple_set_string(t, 1, name);
1289 call_object(tables->branch_type_handler, t, "branch_type_table");
1296 static void python_export_sample_table(struct db_export *dbe,
1297 struct export_sample *es)
1299 struct tables *tables = container_of(dbe, struct tables, dbe);
1304 tuple_set_d64(t, 0, es->db_id);
1305 tuple_set_d64(t, 1, es->evsel->db_id);
1306 tuple_set_d64(t, 2, maps__machine(es->al->maps)->db_id);
1307 tuple_set_d64(t, 3, thread__db_id(es->al->thread));
1308 tuple_set_d64(t, 4, es->comm_db_id);
1309 tuple_set_d64(t, 5, es->dso_db_id);
1310 tuple_set_d64(t, 6, es->sym_db_id);
1311 tuple_set_d64(t, 7, es->offset);
1312 tuple_set_d64(t, 8, es->sample->ip);
1313 tuple_set_d64(t, 9, es->sample->time);
1314 tuple_set_s32(t, 10, es->sample->cpu);
1315 tuple_set_d64(t, 11, es->addr_dso_db_id);
1316 tuple_set_d64(t, 12, es->addr_sym_db_id);
1317 tuple_set_d64(t, 13, es->addr_offset);
1318 tuple_set_d64(t, 14, es->sample->addr);
1319 tuple_set_d64(t, 15, es->sample->period);
1320 tuple_set_d64(t, 16, es->sample->weight);
1321 tuple_set_d64(t, 17, es->sample->transaction);
1322 tuple_set_d64(t, 18, es->sample->data_src);
1323 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
1324 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
1325 tuple_set_d64(t, 21, es->call_path_id);
1326 tuple_set_d64(t, 22, es->sample->insn_cnt);
1327 tuple_set_d64(t, 23, es->sample->cyc_cnt);
1328 tuple_set_s32(t, 24, es->sample->flags);
1330 call_object(tables->sample_handler, t, "sample_table");
1335 static void python_export_synth(struct db_export *dbe, struct export_sample *es)
1337 struct tables *tables = container_of(dbe, struct tables, dbe);
1342 tuple_set_d64(t, 0, es->db_id);
1343 tuple_set_d64(t, 1, es->evsel->core.attr.config);
1344 tuple_set_bytes(t, 2, es->sample->raw_data, es->sample->raw_size);
1346 call_object(tables->synth_handler, t, "synth_data");
1351 static int python_export_sample(struct db_export *dbe,
1352 struct export_sample *es)
1354 struct tables *tables = container_of(dbe, struct tables, dbe);
1356 python_export_sample_table(dbe, es);
1358 if (es->evsel->core.attr.type == PERF_TYPE_SYNTH && tables->synth_handler)
1359 python_export_synth(dbe, es);
1364 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
1366 struct tables *tables = container_of(dbe, struct tables, dbe);
1368 u64 parent_db_id, sym_db_id;
1370 parent_db_id = cp->parent ? cp->parent->db_id : 0;
1371 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
1375 tuple_set_d64(t, 0, cp->db_id);
1376 tuple_set_d64(t, 1, parent_db_id);
1377 tuple_set_d64(t, 2, sym_db_id);
1378 tuple_set_d64(t, 3, cp->ip);
1380 call_object(tables->call_path_handler, t, "call_path_table");
1387 static int python_export_call_return(struct db_export *dbe,
1388 struct call_return *cr)
1390 struct tables *tables = container_of(dbe, struct tables, dbe);
1391 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
1396 tuple_set_d64(t, 0, cr->db_id);
1397 tuple_set_d64(t, 1, thread__db_id(cr->thread));
1398 tuple_set_d64(t, 2, comm_db_id);
1399 tuple_set_d64(t, 3, cr->cp->db_id);
1400 tuple_set_d64(t, 4, cr->call_time);
1401 tuple_set_d64(t, 5, cr->return_time);
1402 tuple_set_d64(t, 6, cr->branch_count);
1403 tuple_set_d64(t, 7, cr->call_ref);
1404 tuple_set_d64(t, 8, cr->return_ref);
1405 tuple_set_d64(t, 9, cr->cp->parent->db_id);
1406 tuple_set_s32(t, 10, cr->flags);
1407 tuple_set_d64(t, 11, cr->parent_db_id);
1408 tuple_set_d64(t, 12, cr->insn_count);
1409 tuple_set_d64(t, 13, cr->cyc_count);
1411 call_object(tables->call_return_handler, t, "call_return_table");
1418 static int python_export_context_switch(struct db_export *dbe, u64 db_id,
1419 struct machine *machine,
1420 struct perf_sample *sample,
1421 u64 th_out_id, u64 comm_out_id,
1422 u64 th_in_id, u64 comm_in_id, int flags)
1424 struct tables *tables = container_of(dbe, struct tables, dbe);
1429 tuple_set_d64(t, 0, db_id);
1430 tuple_set_d64(t, 1, machine->db_id);
1431 tuple_set_d64(t, 2, sample->time);
1432 tuple_set_s32(t, 3, sample->cpu);
1433 tuple_set_d64(t, 4, th_out_id);
1434 tuple_set_d64(t, 5, comm_out_id);
1435 tuple_set_d64(t, 6, th_in_id);
1436 tuple_set_d64(t, 7, comm_in_id);
1437 tuple_set_s32(t, 8, flags);
1439 call_object(tables->context_switch_handler, t, "context_switch");
1446 static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
1449 struct db_export *dbe = data;
1451 return db_export__call_return(dbe, cr, parent_db_id);
1454 static void python_process_general_event(struct perf_sample *sample,
1455 struct evsel *evsel,
1456 struct addr_location *al,
1457 struct addr_location *addr_al)
1459 PyObject *handler, *t, *dict, *callchain;
1460 static char handler_name[64];
1463 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
1465 handler = get_handler(handler_name);
1470 * Use the MAX_FIELDS to make the function expandable, though
1471 * currently there is only one item for the tuple.
1473 t = PyTuple_New(MAX_FIELDS);
1475 Py_FatalError("couldn't create Python tuple");
1478 callchain = python_process_callchain(sample, evsel, al);
1479 dict = get_perf_sample_dict(sample, evsel, al, addr_al, callchain);
1481 PyTuple_SetItem(t, n++, dict);
1482 if (_PyTuple_Resize(&t, n) == -1)
1483 Py_FatalError("error resizing Python tuple");
1485 call_object(handler, t, handler_name);
1490 static void python_process_event(union perf_event *event,
1491 struct perf_sample *sample,
1492 struct evsel *evsel,
1493 struct addr_location *al,
1494 struct addr_location *addr_al)
1496 struct tables *tables = &tables_global;
1498 scripting_context__update(scripting_context, event, sample, evsel, al, addr_al);
1500 switch (evsel->core.attr.type) {
1501 case PERF_TYPE_TRACEPOINT:
1502 python_process_tracepoint(sample, evsel, al, addr_al);
1504 /* Reserve for future process_hw/sw/raw APIs */
1506 if (tables->db_export_mode)
1507 db_export__sample(&tables->dbe, event, sample, evsel, al, addr_al);
1509 python_process_general_event(sample, evsel, al, addr_al);
1513 static void python_process_throttle(union perf_event *event,
1514 struct perf_sample *sample,
1515 struct machine *machine)
1517 const char *handler_name;
1518 PyObject *handler, *t;
1520 if (event->header.type == PERF_RECORD_THROTTLE)
1521 handler_name = "throttle";
1523 handler_name = "unthrottle";
1524 handler = get_handler(handler_name);
1532 tuple_set_u64(t, 0, event->throttle.time);
1533 tuple_set_u64(t, 1, event->throttle.id);
1534 tuple_set_u64(t, 2, event->throttle.stream_id);
1535 tuple_set_s32(t, 3, sample->cpu);
1536 tuple_set_s32(t, 4, sample->pid);
1537 tuple_set_s32(t, 5, sample->tid);
1539 call_object(handler, t, handler_name);
1544 static void python_do_process_switch(union perf_event *event,
1545 struct perf_sample *sample,
1546 struct machine *machine)
1548 const char *handler_name = "context_switch";
1549 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1550 bool out_preempt = out && (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
1551 pid_t np_pid = -1, np_tid = -1;
1552 PyObject *handler, *t;
1554 handler = get_handler(handler_name);
1558 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
1559 np_pid = event->context_switch.next_prev_pid;
1560 np_tid = event->context_switch.next_prev_tid;
1567 tuple_set_u64(t, 0, sample->time);
1568 tuple_set_s32(t, 1, sample->cpu);
1569 tuple_set_s32(t, 2, sample->pid);
1570 tuple_set_s32(t, 3, sample->tid);
1571 tuple_set_s32(t, 4, np_pid);
1572 tuple_set_s32(t, 5, np_tid);
1573 tuple_set_s32(t, 6, machine->pid);
1574 tuple_set_bool(t, 7, out);
1575 tuple_set_bool(t, 8, out_preempt);
1576 tuple_set_s32(t, 9, sample->machine_pid);
1577 tuple_set_s32(t, 10, sample->vcpu);
1579 call_object(handler, t, handler_name);
1584 static void python_process_switch(union perf_event *event,
1585 struct perf_sample *sample,
1586 struct machine *machine)
1588 struct tables *tables = &tables_global;
1590 if (tables->db_export_mode)
1591 db_export__switch(&tables->dbe, event, sample, machine);
1593 python_do_process_switch(event, sample, machine);
1596 static void python_process_auxtrace_error(struct perf_session *session __maybe_unused,
1597 union perf_event *event)
1599 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1600 u8 cpumode = e->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1601 const char *handler_name = "auxtrace_error";
1602 unsigned long long tm = e->time;
1603 const char *msg = e->msg;
1604 PyObject *handler, *t;
1606 handler = get_handler(handler_name);
1612 msg = (const char *)&e->time;
1617 tuple_set_u32(t, 0, e->type);
1618 tuple_set_u32(t, 1, e->code);
1619 tuple_set_s32(t, 2, e->cpu);
1620 tuple_set_s32(t, 3, e->pid);
1621 tuple_set_s32(t, 4, e->tid);
1622 tuple_set_u64(t, 5, e->ip);
1623 tuple_set_u64(t, 6, tm);
1624 tuple_set_string(t, 7, msg);
1625 tuple_set_u32(t, 8, cpumode);
1626 tuple_set_s32(t, 9, e->machine_pid);
1627 tuple_set_s32(t, 10, e->vcpu);
1629 call_object(handler, t, handler_name);
1634 static void get_handler_name(char *str, size_t size,
1635 struct evsel *evsel)
1639 scnprintf(str, size, "stat__%s", evsel__name(evsel));
1641 while ((p = strchr(p, ':'))) {
1648 process_stat(struct evsel *counter, struct perf_cpu cpu, int thread, u64 tstamp,
1649 struct perf_counts_values *count)
1651 PyObject *handler, *t;
1652 static char handler_name[256];
1655 t = PyTuple_New(MAX_FIELDS);
1657 Py_FatalError("couldn't create Python tuple");
1659 get_handler_name(handler_name, sizeof(handler_name),
1662 handler = get_handler(handler_name);
1664 pr_debug("can't find python handler %s\n", handler_name);
1668 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu.cpu));
1669 PyTuple_SetItem(t, n++, _PyLong_FromLong(thread));
1671 tuple_set_u64(t, n++, tstamp);
1672 tuple_set_u64(t, n++, count->val);
1673 tuple_set_u64(t, n++, count->ena);
1674 tuple_set_u64(t, n++, count->run);
1676 if (_PyTuple_Resize(&t, n) == -1)
1677 Py_FatalError("error resizing Python tuple");
1679 call_object(handler, t, handler_name);
1684 static void python_process_stat(struct perf_stat_config *config,
1685 struct evsel *counter, u64 tstamp)
1687 struct perf_thread_map *threads = counter->core.threads;
1688 struct perf_cpu_map *cpus = counter->core.cpus;
1691 for (thread = 0; thread < perf_thread_map__nr(threads); thread++) {
1692 for (cpu = 0; cpu < perf_cpu_map__nr(cpus); cpu++) {
1693 process_stat(counter, perf_cpu_map__cpu(cpus, cpu),
1694 perf_thread_map__pid(threads, thread), tstamp,
1695 perf_counts(counter->counts, cpu, thread));
1700 static void python_process_stat_interval(u64 tstamp)
1702 PyObject *handler, *t;
1703 static const char handler_name[] = "stat__interval";
1706 t = PyTuple_New(MAX_FIELDS);
1708 Py_FatalError("couldn't create Python tuple");
1710 handler = get_handler(handler_name);
1712 pr_debug("can't find python handler %s\n", handler_name);
1716 tuple_set_u64(t, n++, tstamp);
1718 if (_PyTuple_Resize(&t, n) == -1)
1719 Py_FatalError("error resizing Python tuple");
1721 call_object(handler, t, handler_name);
1726 static int perf_script_context_init(void)
1728 PyObject *perf_script_context;
1729 PyObject *perf_trace_context;
1733 perf_trace_context = PyImport_AddModule("perf_trace_context");
1734 if (!perf_trace_context)
1736 dict = PyModule_GetDict(perf_trace_context);
1740 perf_script_context = _PyCapsule_New(scripting_context, NULL, NULL);
1741 if (!perf_script_context)
1744 ret = PyDict_SetItemString(dict, "perf_script_context", perf_script_context);
1746 ret = PyDict_SetItemString(main_dict, "perf_script_context", perf_script_context);
1747 Py_DECREF(perf_script_context);
1751 static int run_start_sub(void)
1753 main_module = PyImport_AddModule("__main__");
1754 if (main_module == NULL)
1756 Py_INCREF(main_module);
1758 main_dict = PyModule_GetDict(main_module);
1759 if (main_dict == NULL)
1761 Py_INCREF(main_dict);
1763 if (perf_script_context_init())
1766 try_call_object("trace_begin", NULL);
1771 Py_XDECREF(main_dict);
1772 Py_XDECREF(main_module);
1776 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1777 tables->handler_name = get_handler(#table_name); \
1778 if (tables->handler_name) \
1779 tables->dbe.export_ ## name = python_export_ ## name; \
1782 #define SET_TABLE_HANDLER(name) \
1783 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1785 static void set_table_handlers(struct tables *tables)
1787 const char *perf_db_export_mode = "perf_db_export_mode";
1788 const char *perf_db_export_calls = "perf_db_export_calls";
1789 const char *perf_db_export_callchains = "perf_db_export_callchains";
1790 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1791 bool export_calls = false;
1792 bool export_callchains = false;
1795 memset(tables, 0, sizeof(struct tables));
1796 if (db_export__init(&tables->dbe))
1797 Py_FatalError("failed to initialize export");
1799 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1800 if (!db_export_mode)
1803 ret = PyObject_IsTrue(db_export_mode);
1805 handler_call_die(perf_db_export_mode);
1809 /* handle export calls */
1810 tables->dbe.crp = NULL;
1811 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1812 if (db_export_calls) {
1813 ret = PyObject_IsTrue(db_export_calls);
1815 handler_call_die(perf_db_export_calls);
1816 export_calls = !!ret;
1821 call_return_processor__new(python_process_call_return,
1823 if (!tables->dbe.crp)
1824 Py_FatalError("failed to create calls processor");
1827 /* handle export callchains */
1828 tables->dbe.cpr = NULL;
1829 db_export_callchains = PyDict_GetItemString(main_dict,
1830 perf_db_export_callchains);
1831 if (db_export_callchains) {
1832 ret = PyObject_IsTrue(db_export_callchains);
1834 handler_call_die(perf_db_export_callchains);
1835 export_callchains = !!ret;
1838 if (export_callchains) {
1840 * Attempt to use the call path root from the call return
1841 * processor, if the call return processor is in use. Otherwise,
1842 * we allocate a new call path root. This prevents exporting
1843 * duplicate call path ids when both are in use simultaneously.
1845 if (tables->dbe.crp)
1846 tables->dbe.cpr = tables->dbe.crp->cpr;
1848 tables->dbe.cpr = call_path_root__new();
1850 if (!tables->dbe.cpr)
1851 Py_FatalError("failed to create call path root");
1854 tables->db_export_mode = true;
1856 * Reserve per symbol space for symbol->db_id via symbol__priv()
1858 symbol_conf.priv_size = sizeof(u64);
1860 SET_TABLE_HANDLER(evsel);
1861 SET_TABLE_HANDLER(machine);
1862 SET_TABLE_HANDLER(thread);
1863 SET_TABLE_HANDLER(comm);
1864 SET_TABLE_HANDLER(comm_thread);
1865 SET_TABLE_HANDLER(dso);
1866 SET_TABLE_HANDLER(symbol);
1867 SET_TABLE_HANDLER(branch_type);
1868 SET_TABLE_HANDLER(sample);
1869 SET_TABLE_HANDLER(call_path);
1870 SET_TABLE_HANDLER(call_return);
1871 SET_TABLE_HANDLER(context_switch);
1874 * Synthesized events are samples but with architecture-specific data
1875 * stored in sample->raw_data. They are exported via
1876 * python_export_sample() and consequently do not need a separate export
1879 tables->synth_handler = get_handler("synth_data");
1882 #if PY_MAJOR_VERSION < 3
1883 static void _free_command_line(const char **command_line, int num)
1888 static void _free_command_line(wchar_t **command_line, int num)
1891 for (i = 0; i < num; i++)
1892 PyMem_RawFree(command_line[i]);
1899 * Start trace script
1901 static int python_start_script(const char *script, int argc, const char **argv,
1902 struct perf_session *session)
1904 struct tables *tables = &tables_global;
1905 #if PY_MAJOR_VERSION < 3
1906 const char **command_line;
1908 wchar_t **command_line;
1911 * Use a non-const name variable to cope with python 2.6's
1912 * PyImport_AppendInittab prototype
1914 char buf[PATH_MAX], name[19] = "perf_trace_context";
1918 scripting_context->session = session;
1919 #if PY_MAJOR_VERSION < 3
1920 command_line = malloc((argc + 1) * sizeof(const char *));
1921 command_line[0] = script;
1922 for (i = 1; i < argc + 1; i++)
1923 command_line[i] = argv[i - 1];
1924 PyImport_AppendInittab(name, initperf_trace_context);
1926 command_line = malloc((argc + 1) * sizeof(wchar_t *));
1927 command_line[0] = Py_DecodeLocale(script, NULL);
1928 for (i = 1; i < argc + 1; i++)
1929 command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
1930 PyImport_AppendInittab(name, PyInit_perf_trace_context);
1934 #if PY_MAJOR_VERSION < 3
1935 PySys_SetArgv(argc + 1, (char **)command_line);
1937 PySys_SetArgv(argc + 1, command_line);
1940 fp = fopen(script, "r");
1942 sprintf(buf, "Can't open python script \"%s\"", script);
1948 err = PyRun_SimpleFile(fp, script);
1950 fprintf(stderr, "Error running python script %s\n", script);
1954 err = run_start_sub();
1956 fprintf(stderr, "Error starting python script %s\n", script);
1960 set_table_handlers(tables);
1962 if (tables->db_export_mode) {
1963 err = db_export__branch_types(&tables->dbe);
1968 _free_command_line(command_line, argc + 1);
1973 _free_command_line(command_line, argc + 1);
1978 static int python_flush_script(void)
1986 static int python_stop_script(void)
1988 struct tables *tables = &tables_global;
1990 try_call_object("trace_end", NULL);
1992 db_export__exit(&tables->dbe);
1994 Py_XDECREF(main_dict);
1995 Py_XDECREF(main_module);
2001 #ifdef HAVE_LIBTRACEEVENT
2002 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
2004 int i, not_first, count, nr_events;
2005 struct tep_event **all_events;
2006 struct tep_event *event = NULL;
2007 struct tep_format_field *f;
2008 char fname[PATH_MAX];
2011 sprintf(fname, "%s.py", outfile);
2012 ofp = fopen(fname, "w");
2014 fprintf(stderr, "couldn't open %s\n", fname);
2017 fprintf(ofp, "# perf script event handlers, "
2018 "generated by perf script -g python\n");
2020 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
2021 " License version 2\n\n");
2023 fprintf(ofp, "# The common_* event handler fields are the most useful "
2024 "fields common to\n");
2026 fprintf(ofp, "# all events. They don't necessarily correspond to "
2027 "the 'common_*' fields\n");
2029 fprintf(ofp, "# in the format files. Those fields not available as "
2030 "handler params can\n");
2032 fprintf(ofp, "# be retrieved using Python functions of the form "
2033 "common_*(context).\n");
2035 fprintf(ofp, "# See the perf-script-python Documentation for the list "
2036 "of available functions.\n\n");
2038 fprintf(ofp, "from __future__ import print_function\n\n");
2039 fprintf(ofp, "import os\n");
2040 fprintf(ofp, "import sys\n\n");
2042 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
2043 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
2044 fprintf(ofp, "\nfrom perf_trace_context import *\n");
2045 fprintf(ofp, "from Core import *\n\n\n");
2047 fprintf(ofp, "def trace_begin():\n");
2048 fprintf(ofp, "\tprint(\"in trace_begin\")\n\n");
2050 fprintf(ofp, "def trace_end():\n");
2051 fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
2053 nr_events = tep_get_events_count(pevent);
2054 all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
2056 for (i = 0; all_events && i < nr_events; i++) {
2057 event = all_events[i];
2058 fprintf(ofp, "def %s__%s(", event->system, event->name);
2059 fprintf(ofp, "event_name, ");
2060 fprintf(ofp, "context, ");
2061 fprintf(ofp, "common_cpu,\n");
2062 fprintf(ofp, "\tcommon_secs, ");
2063 fprintf(ofp, "common_nsecs, ");
2064 fprintf(ofp, "common_pid, ");
2065 fprintf(ofp, "common_comm,\n\t");
2066 fprintf(ofp, "common_callchain, ");
2071 for (f = event->format.fields; f; f = f->next) {
2074 if (++count % 5 == 0)
2075 fprintf(ofp, "\n\t");
2077 fprintf(ofp, "%s", f->name);
2081 if (++count % 5 == 0)
2082 fprintf(ofp, "\n\t\t");
2083 fprintf(ofp, "perf_sample_dict");
2085 fprintf(ofp, "):\n");
2087 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
2088 "common_secs, common_nsecs,\n\t\t\t"
2089 "common_pid, common_comm)\n\n");
2091 fprintf(ofp, "\t\tprint(\"");
2096 for (f = event->format.fields; f; f = f->next) {
2099 if (count && count % 3 == 0) {
2100 fprintf(ofp, "\" \\\n\t\t\"");
2104 fprintf(ofp, "%s=", f->name);
2105 if (f->flags & TEP_FIELD_IS_STRING ||
2106 f->flags & TEP_FIELD_IS_FLAG ||
2107 f->flags & TEP_FIELD_IS_ARRAY ||
2108 f->flags & TEP_FIELD_IS_SYMBOLIC)
2109 fprintf(ofp, "%%s");
2110 else if (f->flags & TEP_FIELD_IS_SIGNED)
2111 fprintf(ofp, "%%d");
2113 fprintf(ofp, "%%u");
2116 fprintf(ofp, "\" %% \\\n\t\t(");
2121 for (f = event->format.fields; f; f = f->next) {
2125 if (++count % 5 == 0)
2126 fprintf(ofp, "\n\t\t");
2128 if (f->flags & TEP_FIELD_IS_FLAG) {
2129 if ((count - 1) % 5 != 0) {
2130 fprintf(ofp, "\n\t\t");
2133 fprintf(ofp, "flag_str(\"");
2134 fprintf(ofp, "%s__%s\", ", event->system,
2136 fprintf(ofp, "\"%s\", %s)", f->name,
2138 } else if (f->flags & TEP_FIELD_IS_SYMBOLIC) {
2139 if ((count - 1) % 5 != 0) {
2140 fprintf(ofp, "\n\t\t");
2143 fprintf(ofp, "symbol_str(\"");
2144 fprintf(ofp, "%s__%s\", ", event->system,
2146 fprintf(ofp, "\"%s\", %s)", f->name,
2149 fprintf(ofp, "%s", f->name);
2152 fprintf(ofp, "))\n\n");
2154 fprintf(ofp, "\t\tprint('Sample: {'+"
2155 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
2157 fprintf(ofp, "\t\tfor node in common_callchain:");
2158 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
2159 fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x] %%s%%s%%s%%s\" %% (");
2160 fprintf(ofp, "\n\t\t\t\t\tnode['ip'], node['sym']['name'],");
2161 fprintf(ofp, "\n\t\t\t\t\t\"+0x{:x}\".format(node['sym_off']) if 'sym_off' in node else \"\",");
2162 fprintf(ofp, "\n\t\t\t\t\t\" ({})\".format(node['dso']) if 'dso' in node else \"\",");
2163 fprintf(ofp, "\n\t\t\t\t\t\" \" + node['sym_srcline'] if 'sym_srcline' in node else \"\"))");
2164 fprintf(ofp, "\n\t\t\telse:");
2165 fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n");
2166 fprintf(ofp, "\t\tprint()\n\n");
2170 fprintf(ofp, "def trace_unhandled(event_name, context, "
2171 "event_fields_dict, perf_sample_dict):\n");
2173 fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n");
2174 fprintf(ofp, "\t\tprint('Sample: {'+"
2175 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
2177 fprintf(ofp, "def print_header("
2178 "event_name, cpu, secs, nsecs, pid, comm):\n"
2179 "\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
2180 "(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n");
2182 fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
2183 "\treturn delimiter.join"
2184 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
2188 fprintf(stderr, "generated Python script: %s\n", fname);
2193 static int python_generate_script(struct tep_handle *pevent __maybe_unused,
2194 const char *outfile __maybe_unused)
2196 fprintf(stderr, "Generating Python perf-script is not supported."
2197 " Install libtraceevent and rebuild perf to enable it.\n"
2198 "For example:\n # apt install libtraceevent-dev (ubuntu)"
2199 "\n # yum install libtraceevent-devel (Fedora)"
2205 struct scripting_ops python_scripting_ops = {
2207 .dirname = "python",
2208 .start_script = python_start_script,
2209 .flush_script = python_flush_script,
2210 .stop_script = python_stop_script,
2211 .process_event = python_process_event,
2212 .process_switch = python_process_switch,
2213 .process_auxtrace_error = python_process_auxtrace_error,
2214 .process_stat = python_process_stat,
2215 .process_stat_interval = python_process_stat_interval,
2216 .process_throttle = python_process_throttle,
2217 .generate_script = python_generate_script,