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"
47 #include "thread_map.h"
49 #include "print_binary.h"
51 #include "mem-events.h"
53 #if PY_MAJOR_VERSION < 3
54 #define _PyUnicode_FromString(arg) \
55 PyString_FromString(arg)
56 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
57 PyString_FromStringAndSize((arg1), (arg2))
58 #define _PyBytes_FromStringAndSize(arg1, arg2) \
59 PyString_FromStringAndSize((arg1), (arg2))
60 #define _PyLong_FromLong(arg) \
62 #define _PyLong_AsLong(arg) \
64 #define _PyCapsule_New(arg1, arg2, arg3) \
65 PyCObject_FromVoidPtr((arg1), (arg2))
67 PyMODINIT_FUNC
initperf_trace_context(void);
69 #define _PyUnicode_FromString(arg) \
70 PyUnicode_FromString(arg)
71 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
72 PyUnicode_FromStringAndSize((arg1), (arg2))
73 #define _PyBytes_FromStringAndSize(arg1, arg2) \
74 PyBytes_FromStringAndSize((arg1), (arg2))
75 #define _PyLong_FromLong(arg) \
77 #define _PyLong_AsLong(arg) \
79 #define _PyCapsule_New(arg1, arg2, arg3) \
80 PyCapsule_New((arg1), (arg2), (arg3))
82 PyMODINIT_FUNC
PyInit_perf_trace_context(void);
85 #define TRACE_EVENT_TYPE_MAX \
86 ((1 << (sizeof(unsigned short) * 8)) - 1)
88 static DECLARE_BITMAP(events_defined
, TRACE_EVENT_TYPE_MAX
);
91 #define N_COMMON_FIELDS 7
93 extern struct scripting_context
*scripting_context
;
95 static char *cur_field_name
;
96 static int zero_flag_atom
;
98 static PyObject
*main_module
, *main_dict
;
101 struct db_export dbe
;
102 PyObject
*evsel_handler
;
103 PyObject
*machine_handler
;
104 PyObject
*thread_handler
;
105 PyObject
*comm_handler
;
106 PyObject
*comm_thread_handler
;
107 PyObject
*dso_handler
;
108 PyObject
*symbol_handler
;
109 PyObject
*branch_type_handler
;
110 PyObject
*sample_handler
;
111 PyObject
*call_path_handler
;
112 PyObject
*call_return_handler
;
116 static struct tables tables_global
;
118 static void handler_call_die(const char *handler_name
) __noreturn
;
119 static void handler_call_die(const char *handler_name
)
122 Py_FatalError("problem in Python trace event handler");
123 // Py_FatalError does not return
124 // but we have to make the compiler happy
129 * Insert val into into the dictionary and decrement the reference counter.
130 * This is necessary for dictionaries since PyDict_SetItemString() does not
131 * steal a reference, as opposed to PyTuple_SetItem().
133 static void pydict_set_item_string_decref(PyObject
*dict
, const char *key
, PyObject
*val
)
135 PyDict_SetItemString(dict
, key
, val
);
139 static PyObject
*get_handler(const char *handler_name
)
143 handler
= PyDict_GetItemString(main_dict
, handler_name
);
144 if (handler
&& !PyCallable_Check(handler
))
149 static int get_argument_count(PyObject
*handler
)
154 * The attribute for the code object is func_code in Python 2,
155 * whereas it is __code__ in Python 3.0+.
157 PyObject
*code_obj
= PyObject_GetAttrString(handler
,
159 if (PyErr_Occurred()) {
161 code_obj
= PyObject_GetAttrString(handler
,
166 PyObject
*arg_count_obj
= PyObject_GetAttrString(code_obj
,
169 arg_count
= (int) _PyLong_AsLong(arg_count_obj
);
170 Py_DECREF(arg_count_obj
);
177 static void call_object(PyObject
*handler
, PyObject
*args
, const char *die_msg
)
181 retval
= PyObject_CallObject(handler
, args
);
183 handler_call_die(die_msg
);
187 static void try_call_object(const char *handler_name
, PyObject
*args
)
191 handler
= get_handler(handler_name
);
193 call_object(handler
, args
, handler_name
);
196 static void define_value(enum print_arg_type field_type
,
198 const char *field_name
,
199 const char *field_value
,
200 const char *field_str
)
202 const char *handler_name
= "define_flag_value";
204 unsigned long long value
;
207 if (field_type
== PRINT_SYMBOL
)
208 handler_name
= "define_symbolic_value";
212 Py_FatalError("couldn't create Python tuple");
214 value
= eval_flag(field_value
);
216 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(ev_name
));
217 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(field_name
));
218 PyTuple_SetItem(t
, n
++, _PyLong_FromLong(value
));
219 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(field_str
));
221 try_call_object(handler_name
, t
);
226 static void define_values(enum print_arg_type field_type
,
227 struct print_flag_sym
*field
,
229 const char *field_name
)
231 define_value(field_type
, ev_name
, field_name
, field
->value
,
235 define_values(field_type
, field
->next
, ev_name
, field_name
);
238 static void define_field(enum print_arg_type field_type
,
240 const char *field_name
,
243 const char *handler_name
= "define_flag_field";
247 if (field_type
== PRINT_SYMBOL
)
248 handler_name
= "define_symbolic_field";
250 if (field_type
== PRINT_FLAGS
)
255 Py_FatalError("couldn't create Python tuple");
257 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(ev_name
));
258 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(field_name
));
259 if (field_type
== PRINT_FLAGS
)
260 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(delim
));
262 try_call_object(handler_name
, t
);
267 static void define_event_symbols(struct event_format
*event
,
269 struct print_arg
*args
)
274 switch (args
->type
) {
278 define_value(PRINT_FLAGS
, ev_name
, cur_field_name
, "0",
283 free(cur_field_name
);
284 cur_field_name
= strdup(args
->field
.name
);
287 define_event_symbols(event
, ev_name
, args
->flags
.field
);
288 define_field(PRINT_FLAGS
, ev_name
, cur_field_name
,
290 define_values(PRINT_FLAGS
, args
->flags
.flags
, ev_name
,
294 define_event_symbols(event
, ev_name
, args
->symbol
.field
);
295 define_field(PRINT_SYMBOL
, ev_name
, cur_field_name
, NULL
);
296 define_values(PRINT_SYMBOL
, args
->symbol
.symbols
, ev_name
,
301 define_event_symbols(event
, ev_name
, args
->hex
.field
);
302 define_event_symbols(event
, ev_name
, args
->hex
.size
);
304 case PRINT_INT_ARRAY
:
305 define_event_symbols(event
, ev_name
, args
->int_array
.field
);
306 define_event_symbols(event
, ev_name
, args
->int_array
.count
);
307 define_event_symbols(event
, ev_name
, args
->int_array
.el_size
);
312 define_event_symbols(event
, ev_name
, args
->typecast
.item
);
315 if (strcmp(args
->op
.op
, ":") == 0)
317 define_event_symbols(event
, ev_name
, args
->op
.left
);
318 define_event_symbols(event
, ev_name
, args
->op
.right
);
321 /* gcc warns for these? */
323 case PRINT_DYNAMIC_ARRAY
:
324 case PRINT_DYNAMIC_ARRAY_LEN
:
327 /* we should warn... */
332 define_event_symbols(event
, ev_name
, args
->next
);
335 static PyObject
*get_field_numeric_entry(struct event_format
*event
,
336 struct format_field
*field
, void *data
)
338 bool is_array
= field
->flags
& FIELD_IS_ARRAY
;
339 PyObject
*obj
= NULL
, *list
= NULL
;
340 unsigned long long val
;
341 unsigned int item_size
, n_items
, i
;
344 list
= PyList_New(field
->arraylen
);
345 item_size
= field
->size
/ field
->arraylen
;
346 n_items
= field
->arraylen
;
348 item_size
= field
->size
;
352 for (i
= 0; i
< n_items
; i
++) {
354 val
= read_size(event
, data
+ field
->offset
+ i
* item_size
,
356 if (field
->flags
& FIELD_IS_SIGNED
) {
357 if ((long long)val
>= LONG_MIN
&&
358 (long long)val
<= LONG_MAX
)
359 obj
= _PyLong_FromLong(val
);
361 obj
= PyLong_FromLongLong(val
);
364 obj
= _PyLong_FromLong(val
);
366 obj
= PyLong_FromUnsignedLongLong(val
);
369 PyList_SET_ITEM(list
, i
, obj
);
376 static const char *get_dsoname(struct map
*map
)
378 const char *dsoname
= "[unknown]";
380 if (map
&& map
->dso
) {
381 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
382 dsoname
= map
->dso
->long_name
;
384 dsoname
= map
->dso
->name
;
390 static PyObject
*python_process_callchain(struct perf_sample
*sample
,
391 struct perf_evsel
*evsel
,
392 struct addr_location
*al
)
396 pylist
= PyList_New(0);
398 Py_FatalError("couldn't create Python list");
400 if (!symbol_conf
.use_callchain
|| !sample
->callchain
)
403 if (thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
405 scripting_max_stack
) != 0) {
406 pr_err("Failed to resolve callchain. Skipping\n");
409 callchain_cursor_commit(&callchain_cursor
);
414 struct callchain_cursor_node
*node
;
415 node
= callchain_cursor_current(&callchain_cursor
);
419 pyelem
= PyDict_New();
421 Py_FatalError("couldn't create Python dictionary");
424 pydict_set_item_string_decref(pyelem
, "ip",
425 PyLong_FromUnsignedLongLong(node
->ip
));
428 PyObject
*pysym
= PyDict_New();
430 Py_FatalError("couldn't create Python dictionary");
431 pydict_set_item_string_decref(pysym
, "start",
432 PyLong_FromUnsignedLongLong(node
->sym
->start
));
433 pydict_set_item_string_decref(pysym
, "end",
434 PyLong_FromUnsignedLongLong(node
->sym
->end
));
435 pydict_set_item_string_decref(pysym
, "binding",
436 _PyLong_FromLong(node
->sym
->binding
));
437 pydict_set_item_string_decref(pysym
, "name",
438 _PyUnicode_FromStringAndSize(node
->sym
->name
,
439 node
->sym
->namelen
));
440 pydict_set_item_string_decref(pyelem
, "sym", pysym
);
444 const char *dsoname
= get_dsoname(node
->map
);
446 pydict_set_item_string_decref(pyelem
, "dso",
447 _PyUnicode_FromString(dsoname
));
450 callchain_cursor_advance(&callchain_cursor
);
451 PyList_Append(pylist
, pyelem
);
459 static PyObject
*python_process_brstack(struct perf_sample
*sample
,
460 struct thread
*thread
)
462 struct branch_stack
*br
= sample
->branch_stack
;
466 pylist
= PyList_New(0);
468 Py_FatalError("couldn't create Python list");
473 for (i
= 0; i
< br
->nr
; i
++) {
475 struct addr_location al
;
478 pyelem
= PyDict_New();
480 Py_FatalError("couldn't create Python dictionary");
482 pydict_set_item_string_decref(pyelem
, "from",
483 PyLong_FromUnsignedLongLong(br
->entries
[i
].from
));
484 pydict_set_item_string_decref(pyelem
, "to",
485 PyLong_FromUnsignedLongLong(br
->entries
[i
].to
));
486 pydict_set_item_string_decref(pyelem
, "mispred",
487 PyBool_FromLong(br
->entries
[i
].flags
.mispred
));
488 pydict_set_item_string_decref(pyelem
, "predicted",
489 PyBool_FromLong(br
->entries
[i
].flags
.predicted
));
490 pydict_set_item_string_decref(pyelem
, "in_tx",
491 PyBool_FromLong(br
->entries
[i
].flags
.in_tx
));
492 pydict_set_item_string_decref(pyelem
, "abort",
493 PyBool_FromLong(br
->entries
[i
].flags
.abort
));
494 pydict_set_item_string_decref(pyelem
, "cycles",
495 PyLong_FromUnsignedLongLong(br
->entries
[i
].flags
.cycles
));
497 thread__find_map_fb(thread
, sample
->cpumode
,
498 br
->entries
[i
].from
, &al
);
499 dsoname
= get_dsoname(al
.map
);
500 pydict_set_item_string_decref(pyelem
, "from_dsoname",
501 _PyUnicode_FromString(dsoname
));
503 thread__find_map_fb(thread
, sample
->cpumode
,
504 br
->entries
[i
].to
, &al
);
505 dsoname
= get_dsoname(al
.map
);
506 pydict_set_item_string_decref(pyelem
, "to_dsoname",
507 _PyUnicode_FromString(dsoname
));
509 PyList_Append(pylist
, pyelem
);
517 static unsigned long get_offset(struct symbol
*sym
, struct addr_location
*al
)
519 unsigned long offset
;
521 if (al
->addr
< sym
->end
)
522 offset
= al
->addr
- sym
->start
;
524 offset
= al
->addr
- al
->map
->start
- sym
->start
;
529 static int get_symoff(struct symbol
*sym
, struct addr_location
*al
,
530 bool print_off
, char *bf
, int size
)
532 unsigned long offset
;
534 if (!sym
|| !sym
->name
[0])
535 return scnprintf(bf
, size
, "%s", "[unknown]");
538 return scnprintf(bf
, size
, "%s", sym
->name
);
540 offset
= get_offset(sym
, al
);
542 return scnprintf(bf
, size
, "%s+0x%x", sym
->name
, offset
);
545 static int get_br_mspred(struct branch_flags
*flags
, char *bf
, int size
)
547 if (!flags
->mispred
&& !flags
->predicted
)
548 return scnprintf(bf
, size
, "%s", "-");
551 return scnprintf(bf
, size
, "%s", "M");
553 return scnprintf(bf
, size
, "%s", "P");
556 static PyObject
*python_process_brstacksym(struct perf_sample
*sample
,
557 struct thread
*thread
)
559 struct branch_stack
*br
= sample
->branch_stack
;
563 struct addr_location al
;
565 pylist
= PyList_New(0);
567 Py_FatalError("couldn't create Python list");
572 for (i
= 0; i
< br
->nr
; i
++) {
575 pyelem
= PyDict_New();
577 Py_FatalError("couldn't create Python dictionary");
579 thread__find_symbol_fb(thread
, sample
->cpumode
,
580 br
->entries
[i
].from
, &al
);
581 get_symoff(al
.sym
, &al
, true, bf
, sizeof(bf
));
582 pydict_set_item_string_decref(pyelem
, "from",
583 _PyUnicode_FromString(bf
));
585 thread__find_symbol_fb(thread
, sample
->cpumode
,
586 br
->entries
[i
].to
, &al
);
587 get_symoff(al
.sym
, &al
, true, bf
, sizeof(bf
));
588 pydict_set_item_string_decref(pyelem
, "to",
589 _PyUnicode_FromString(bf
));
591 get_br_mspred(&br
->entries
[i
].flags
, bf
, sizeof(bf
));
592 pydict_set_item_string_decref(pyelem
, "pred",
593 _PyUnicode_FromString(bf
));
595 if (br
->entries
[i
].flags
.in_tx
) {
596 pydict_set_item_string_decref(pyelem
, "in_tx",
597 _PyUnicode_FromString("X"));
599 pydict_set_item_string_decref(pyelem
, "in_tx",
600 _PyUnicode_FromString("-"));
603 if (br
->entries
[i
].flags
.abort
) {
604 pydict_set_item_string_decref(pyelem
, "abort",
605 _PyUnicode_FromString("A"));
607 pydict_set_item_string_decref(pyelem
, "abort",
608 _PyUnicode_FromString("-"));
611 PyList_Append(pylist
, pyelem
);
619 static PyObject
*get_sample_value_as_tuple(struct sample_read_value
*value
)
625 Py_FatalError("couldn't create Python tuple");
626 PyTuple_SetItem(t
, 0, PyLong_FromUnsignedLongLong(value
->id
));
627 PyTuple_SetItem(t
, 1, PyLong_FromUnsignedLongLong(value
->value
));
631 static void set_sample_read_in_dict(PyObject
*dict_sample
,
632 struct perf_sample
*sample
,
633 struct perf_evsel
*evsel
)
635 u64 read_format
= evsel
->attr
.read_format
;
639 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
640 pydict_set_item_string_decref(dict_sample
, "time_enabled",
641 PyLong_FromUnsignedLongLong(sample
->read
.time_enabled
));
644 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
645 pydict_set_item_string_decref(dict_sample
, "time_running",
646 PyLong_FromUnsignedLongLong(sample
->read
.time_running
));
649 if (read_format
& PERF_FORMAT_GROUP
)
650 values
= PyList_New(sample
->read
.group
.nr
);
652 values
= PyList_New(1);
655 Py_FatalError("couldn't create Python list");
657 if (read_format
& PERF_FORMAT_GROUP
) {
658 for (i
= 0; i
< sample
->read
.group
.nr
; i
++) {
659 PyObject
*t
= get_sample_value_as_tuple(&sample
->read
.group
.values
[i
]);
660 PyList_SET_ITEM(values
, i
, t
);
663 PyObject
*t
= get_sample_value_as_tuple(&sample
->read
.one
);
664 PyList_SET_ITEM(values
, 0, t
);
666 pydict_set_item_string_decref(dict_sample
, "values", values
);
669 static void set_sample_datasrc_in_dict(PyObject
*dict
,
670 struct perf_sample
*sample
)
672 struct mem_info mi
= { .data_src
.val
= sample
->data_src
};
675 pydict_set_item_string_decref(dict
, "datasrc",
676 PyLong_FromUnsignedLongLong(sample
->data_src
));
678 perf_script__meminfo_scnprintf(decode
, 100, &mi
);
680 pydict_set_item_string_decref(dict
, "datasrc_decode",
681 _PyUnicode_FromString(decode
));
684 static int regs_map(struct regs_dump
*regs
, uint64_t mask
, char *bf
, int size
)
686 unsigned int i
= 0, r
;
691 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
692 u64 val
= regs
->regs
[i
++];
694 printed
+= scnprintf(bf
+ printed
, size
- printed
,
695 "%5s:0x%" PRIx64
" ",
696 perf_reg_name(r
), val
);
702 static void set_regs_in_dict(PyObject
*dict
,
703 struct perf_sample
*sample
,
704 struct perf_evsel
*evsel
)
706 struct perf_event_attr
*attr
= &evsel
->attr
;
709 regs_map(&sample
->intr_regs
, attr
->sample_regs_intr
, bf
, sizeof(bf
));
711 pydict_set_item_string_decref(dict
, "iregs",
712 _PyUnicode_FromString(bf
));
714 regs_map(&sample
->user_regs
, attr
->sample_regs_user
, bf
, sizeof(bf
));
716 pydict_set_item_string_decref(dict
, "uregs",
717 _PyUnicode_FromString(bf
));
720 static PyObject
*get_perf_sample_dict(struct perf_sample
*sample
,
721 struct perf_evsel
*evsel
,
722 struct addr_location
*al
,
725 PyObject
*dict
, *dict_sample
, *brstack
, *brstacksym
;
729 Py_FatalError("couldn't create Python dictionary");
731 dict_sample
= PyDict_New();
733 Py_FatalError("couldn't create Python dictionary");
735 pydict_set_item_string_decref(dict
, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel
)));
736 pydict_set_item_string_decref(dict
, "attr", _PyUnicode_FromStringAndSize(
737 (const char *)&evsel
->attr
, sizeof(evsel
->attr
)));
739 pydict_set_item_string_decref(dict_sample
, "pid",
740 _PyLong_FromLong(sample
->pid
));
741 pydict_set_item_string_decref(dict_sample
, "tid",
742 _PyLong_FromLong(sample
->tid
));
743 pydict_set_item_string_decref(dict_sample
, "cpu",
744 _PyLong_FromLong(sample
->cpu
));
745 pydict_set_item_string_decref(dict_sample
, "ip",
746 PyLong_FromUnsignedLongLong(sample
->ip
));
747 pydict_set_item_string_decref(dict_sample
, "time",
748 PyLong_FromUnsignedLongLong(sample
->time
));
749 pydict_set_item_string_decref(dict_sample
, "period",
750 PyLong_FromUnsignedLongLong(sample
->period
));
751 pydict_set_item_string_decref(dict_sample
, "phys_addr",
752 PyLong_FromUnsignedLongLong(sample
->phys_addr
));
753 pydict_set_item_string_decref(dict_sample
, "addr",
754 PyLong_FromUnsignedLongLong(sample
->addr
));
755 set_sample_read_in_dict(dict_sample
, sample
, evsel
);
756 pydict_set_item_string_decref(dict_sample
, "weight",
757 PyLong_FromUnsignedLongLong(sample
->weight
));
758 pydict_set_item_string_decref(dict_sample
, "transaction",
759 PyLong_FromUnsignedLongLong(sample
->transaction
));
760 set_sample_datasrc_in_dict(dict_sample
, sample
);
761 pydict_set_item_string_decref(dict
, "sample", dict_sample
);
763 pydict_set_item_string_decref(dict
, "raw_buf", _PyBytes_FromStringAndSize(
764 (const char *)sample
->raw_data
, sample
->raw_size
));
765 pydict_set_item_string_decref(dict
, "comm",
766 _PyUnicode_FromString(thread__comm_str(al
->thread
)));
768 pydict_set_item_string_decref(dict
, "dso",
769 _PyUnicode_FromString(al
->map
->dso
->name
));
772 pydict_set_item_string_decref(dict
, "symbol",
773 _PyUnicode_FromString(al
->sym
->name
));
776 pydict_set_item_string_decref(dict
, "callchain", callchain
);
778 brstack
= python_process_brstack(sample
, al
->thread
);
779 pydict_set_item_string_decref(dict
, "brstack", brstack
);
781 brstacksym
= python_process_brstacksym(sample
, al
->thread
);
782 pydict_set_item_string_decref(dict
, "brstacksym", brstacksym
);
784 set_regs_in_dict(dict
, sample
, evsel
);
789 static void python_process_tracepoint(struct perf_sample
*sample
,
790 struct perf_evsel
*evsel
,
791 struct addr_location
*al
)
793 struct event_format
*event
= evsel
->tp_format
;
794 PyObject
*handler
, *context
, *t
, *obj
= NULL
, *callchain
;
795 PyObject
*dict
= NULL
, *all_entries_dict
= NULL
;
796 static char handler_name
[256];
797 struct format_field
*field
;
801 int cpu
= sample
->cpu
;
802 void *data
= sample
->raw_data
;
803 unsigned long long nsecs
= sample
->time
;
804 const char *comm
= thread__comm_str(al
->thread
);
805 const char *default_handler_name
= "trace_unhandled";
808 snprintf(handler_name
, sizeof(handler_name
),
809 "ug! no event found for type %" PRIu64
, (u64
)evsel
->attr
.config
);
810 Py_FatalError(handler_name
);
813 pid
= raw_field_value(event
, "common_pid", data
);
815 sprintf(handler_name
, "%s__%s", event
->system
, event
->name
);
817 if (!test_and_set_bit(event
->id
, events_defined
))
818 define_event_symbols(event
, handler_name
, event
->print_fmt
.args
);
820 handler
= get_handler(handler_name
);
822 handler
= get_handler(default_handler_name
);
827 Py_FatalError("couldn't create Python dict");
830 t
= PyTuple_New(MAX_FIELDS
);
832 Py_FatalError("couldn't create Python tuple");
835 s
= nsecs
/ NSEC_PER_SEC
;
836 ns
= nsecs
- s
* NSEC_PER_SEC
;
838 scripting_context
->event_data
= data
;
839 scripting_context
->pevent
= evsel
->tp_format
->pevent
;
841 context
= _PyCapsule_New(scripting_context
, NULL
, NULL
);
843 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(handler_name
));
844 PyTuple_SetItem(t
, n
++, context
);
847 callchain
= python_process_callchain(sample
, evsel
, al
);
848 /* Need an additional reference for the perf_sample dict */
849 Py_INCREF(callchain
);
852 PyTuple_SetItem(t
, n
++, _PyLong_FromLong(cpu
));
853 PyTuple_SetItem(t
, n
++, _PyLong_FromLong(s
));
854 PyTuple_SetItem(t
, n
++, _PyLong_FromLong(ns
));
855 PyTuple_SetItem(t
, n
++, _PyLong_FromLong(pid
));
856 PyTuple_SetItem(t
, n
++, _PyUnicode_FromString(comm
));
857 PyTuple_SetItem(t
, n
++, callchain
);
859 pydict_set_item_string_decref(dict
, "common_cpu", _PyLong_FromLong(cpu
));
860 pydict_set_item_string_decref(dict
, "common_s", _PyLong_FromLong(s
));
861 pydict_set_item_string_decref(dict
, "common_ns", _PyLong_FromLong(ns
));
862 pydict_set_item_string_decref(dict
, "common_pid", _PyLong_FromLong(pid
));
863 pydict_set_item_string_decref(dict
, "common_comm", _PyUnicode_FromString(comm
));
864 pydict_set_item_string_decref(dict
, "common_callchain", callchain
);
866 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
867 unsigned int offset
, len
;
868 unsigned long long val
;
870 if (field
->flags
& FIELD_IS_ARRAY
) {
871 offset
= field
->offset
;
873 if (field
->flags
& FIELD_IS_DYNAMIC
) {
874 val
= tep_read_number(scripting_context
->pevent
,
880 if (field
->flags
& FIELD_IS_STRING
&&
881 is_printable_array(data
+ offset
, len
)) {
882 obj
= _PyUnicode_FromString((char *) data
+ offset
);
884 obj
= PyByteArray_FromStringAndSize((const char *) data
+ offset
, len
);
885 field
->flags
&= ~FIELD_IS_STRING
;
887 } else { /* FIELD_IS_NUMERIC */
888 obj
= get_field_numeric_entry(event
, field
, data
);
891 PyTuple_SetItem(t
, n
++, obj
);
893 pydict_set_item_string_decref(dict
, field
->name
, obj
);
898 PyTuple_SetItem(t
, n
++, dict
);
900 if (get_argument_count(handler
) == (int) n
+ 1) {
901 all_entries_dict
= get_perf_sample_dict(sample
, evsel
, al
,
903 PyTuple_SetItem(t
, n
++, all_entries_dict
);
905 Py_DECREF(callchain
);
908 if (_PyTuple_Resize(&t
, n
) == -1)
909 Py_FatalError("error resizing Python tuple");
912 call_object(handler
, t
, handler_name
);
914 call_object(handler
, t
, default_handler_name
);
919 static PyObject
*tuple_new(unsigned int sz
)
925 Py_FatalError("couldn't create Python tuple");
929 static int tuple_set_u64(PyObject
*t
, unsigned int pos
, u64 val
)
931 #if BITS_PER_LONG == 64
932 return PyTuple_SetItem(t
, pos
, _PyLong_FromLong(val
));
934 #if BITS_PER_LONG == 32
935 return PyTuple_SetItem(t
, pos
, PyLong_FromLongLong(val
));
939 static int tuple_set_s32(PyObject
*t
, unsigned int pos
, s32 val
)
941 return PyTuple_SetItem(t
, pos
, _PyLong_FromLong(val
));
944 static int tuple_set_string(PyObject
*t
, unsigned int pos
, const char *s
)
946 return PyTuple_SetItem(t
, pos
, _PyUnicode_FromString(s
));
949 static int python_export_evsel(struct db_export
*dbe
, struct perf_evsel
*evsel
)
951 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
956 tuple_set_u64(t
, 0, evsel
->db_id
);
957 tuple_set_string(t
, 1, perf_evsel__name(evsel
));
959 call_object(tables
->evsel_handler
, t
, "evsel_table");
966 static int python_export_machine(struct db_export
*dbe
,
967 struct machine
*machine
)
969 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
974 tuple_set_u64(t
, 0, machine
->db_id
);
975 tuple_set_s32(t
, 1, machine
->pid
);
976 tuple_set_string(t
, 2, machine
->root_dir
? machine
->root_dir
: "");
978 call_object(tables
->machine_handler
, t
, "machine_table");
985 static int python_export_thread(struct db_export
*dbe
, struct thread
*thread
,
986 u64 main_thread_db_id
, struct machine
*machine
)
988 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
993 tuple_set_u64(t
, 0, thread
->db_id
);
994 tuple_set_u64(t
, 1, machine
->db_id
);
995 tuple_set_u64(t
, 2, main_thread_db_id
);
996 tuple_set_s32(t
, 3, thread
->pid_
);
997 tuple_set_s32(t
, 4, thread
->tid
);
999 call_object(tables
->thread_handler
, t
, "thread_table");
1006 static int python_export_comm(struct db_export
*dbe
, struct comm
*comm
)
1008 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1013 tuple_set_u64(t
, 0, comm
->db_id
);
1014 tuple_set_string(t
, 1, comm__str(comm
));
1016 call_object(tables
->comm_handler
, t
, "comm_table");
1023 static int python_export_comm_thread(struct db_export
*dbe
, u64 db_id
,
1024 struct comm
*comm
, struct thread
*thread
)
1026 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1031 tuple_set_u64(t
, 0, db_id
);
1032 tuple_set_u64(t
, 1, comm
->db_id
);
1033 tuple_set_u64(t
, 2, thread
->db_id
);
1035 call_object(tables
->comm_thread_handler
, t
, "comm_thread_table");
1042 static int python_export_dso(struct db_export
*dbe
, struct dso
*dso
,
1043 struct machine
*machine
)
1045 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1046 char sbuild_id
[SBUILD_ID_SIZE
];
1049 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1053 tuple_set_u64(t
, 0, dso
->db_id
);
1054 tuple_set_u64(t
, 1, machine
->db_id
);
1055 tuple_set_string(t
, 2, dso
->short_name
);
1056 tuple_set_string(t
, 3, dso
->long_name
);
1057 tuple_set_string(t
, 4, sbuild_id
);
1059 call_object(tables
->dso_handler
, t
, "dso_table");
1066 static int python_export_symbol(struct db_export
*dbe
, struct symbol
*sym
,
1069 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1070 u64
*sym_db_id
= symbol__priv(sym
);
1075 tuple_set_u64(t
, 0, *sym_db_id
);
1076 tuple_set_u64(t
, 1, dso
->db_id
);
1077 tuple_set_u64(t
, 2, sym
->start
);
1078 tuple_set_u64(t
, 3, sym
->end
);
1079 tuple_set_s32(t
, 4, sym
->binding
);
1080 tuple_set_string(t
, 5, sym
->name
);
1082 call_object(tables
->symbol_handler
, t
, "symbol_table");
1089 static int python_export_branch_type(struct db_export
*dbe
, u32 branch_type
,
1092 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1097 tuple_set_s32(t
, 0, branch_type
);
1098 tuple_set_string(t
, 1, name
);
1100 call_object(tables
->branch_type_handler
, t
, "branch_type_table");
1107 static int python_export_sample(struct db_export
*dbe
,
1108 struct export_sample
*es
)
1110 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1115 tuple_set_u64(t
, 0, es
->db_id
);
1116 tuple_set_u64(t
, 1, es
->evsel
->db_id
);
1117 tuple_set_u64(t
, 2, es
->al
->machine
->db_id
);
1118 tuple_set_u64(t
, 3, es
->al
->thread
->db_id
);
1119 tuple_set_u64(t
, 4, es
->comm_db_id
);
1120 tuple_set_u64(t
, 5, es
->dso_db_id
);
1121 tuple_set_u64(t
, 6, es
->sym_db_id
);
1122 tuple_set_u64(t
, 7, es
->offset
);
1123 tuple_set_u64(t
, 8, es
->sample
->ip
);
1124 tuple_set_u64(t
, 9, es
->sample
->time
);
1125 tuple_set_s32(t
, 10, es
->sample
->cpu
);
1126 tuple_set_u64(t
, 11, es
->addr_dso_db_id
);
1127 tuple_set_u64(t
, 12, es
->addr_sym_db_id
);
1128 tuple_set_u64(t
, 13, es
->addr_offset
);
1129 tuple_set_u64(t
, 14, es
->sample
->addr
);
1130 tuple_set_u64(t
, 15, es
->sample
->period
);
1131 tuple_set_u64(t
, 16, es
->sample
->weight
);
1132 tuple_set_u64(t
, 17, es
->sample
->transaction
);
1133 tuple_set_u64(t
, 18, es
->sample
->data_src
);
1134 tuple_set_s32(t
, 19, es
->sample
->flags
& PERF_BRANCH_MASK
);
1135 tuple_set_s32(t
, 20, !!(es
->sample
->flags
& PERF_IP_FLAG_IN_TX
));
1136 tuple_set_u64(t
, 21, es
->call_path_id
);
1138 call_object(tables
->sample_handler
, t
, "sample_table");
1145 static int python_export_call_path(struct db_export
*dbe
, struct call_path
*cp
)
1147 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1149 u64 parent_db_id
, sym_db_id
;
1151 parent_db_id
= cp
->parent
? cp
->parent
->db_id
: 0;
1152 sym_db_id
= cp
->sym
? *(u64
*)symbol__priv(cp
->sym
) : 0;
1156 tuple_set_u64(t
, 0, cp
->db_id
);
1157 tuple_set_u64(t
, 1, parent_db_id
);
1158 tuple_set_u64(t
, 2, sym_db_id
);
1159 tuple_set_u64(t
, 3, cp
->ip
);
1161 call_object(tables
->call_path_handler
, t
, "call_path_table");
1168 static int python_export_call_return(struct db_export
*dbe
,
1169 struct call_return
*cr
)
1171 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
1172 u64 comm_db_id
= cr
->comm
? cr
->comm
->db_id
: 0;
1177 tuple_set_u64(t
, 0, cr
->db_id
);
1178 tuple_set_u64(t
, 1, cr
->thread
->db_id
);
1179 tuple_set_u64(t
, 2, comm_db_id
);
1180 tuple_set_u64(t
, 3, cr
->cp
->db_id
);
1181 tuple_set_u64(t
, 4, cr
->call_time
);
1182 tuple_set_u64(t
, 5, cr
->return_time
);
1183 tuple_set_u64(t
, 6, cr
->branch_count
);
1184 tuple_set_u64(t
, 7, cr
->call_ref
);
1185 tuple_set_u64(t
, 8, cr
->return_ref
);
1186 tuple_set_u64(t
, 9, cr
->cp
->parent
->db_id
);
1187 tuple_set_s32(t
, 10, cr
->flags
);
1189 call_object(tables
->call_return_handler
, t
, "call_return_table");
1196 static int python_process_call_return(struct call_return
*cr
, void *data
)
1198 struct db_export
*dbe
= data
;
1200 return db_export__call_return(dbe
, cr
);
1203 static void python_process_general_event(struct perf_sample
*sample
,
1204 struct perf_evsel
*evsel
,
1205 struct addr_location
*al
)
1207 PyObject
*handler
, *t
, *dict
, *callchain
;
1208 static char handler_name
[64];
1211 snprintf(handler_name
, sizeof(handler_name
), "%s", "process_event");
1213 handler
= get_handler(handler_name
);
1218 * Use the MAX_FIELDS to make the function expandable, though
1219 * currently there is only one item for the tuple.
1221 t
= PyTuple_New(MAX_FIELDS
);
1223 Py_FatalError("couldn't create Python tuple");
1226 callchain
= python_process_callchain(sample
, evsel
, al
);
1227 dict
= get_perf_sample_dict(sample
, evsel
, al
, callchain
);
1229 PyTuple_SetItem(t
, n
++, dict
);
1230 if (_PyTuple_Resize(&t
, n
) == -1)
1231 Py_FatalError("error resizing Python tuple");
1233 call_object(handler
, t
, handler_name
);
1238 static void python_process_event(union perf_event
*event
,
1239 struct perf_sample
*sample
,
1240 struct perf_evsel
*evsel
,
1241 struct addr_location
*al
)
1243 struct tables
*tables
= &tables_global
;
1245 switch (evsel
->attr
.type
) {
1246 case PERF_TYPE_TRACEPOINT
:
1247 python_process_tracepoint(sample
, evsel
, al
);
1249 /* Reserve for future process_hw/sw/raw APIs */
1251 if (tables
->db_export_mode
)
1252 db_export__sample(&tables
->dbe
, event
, sample
, evsel
, al
);
1254 python_process_general_event(sample
, evsel
, al
);
1258 static void get_handler_name(char *str
, size_t size
,
1259 struct perf_evsel
*evsel
)
1263 scnprintf(str
, size
, "stat__%s", perf_evsel__name(evsel
));
1265 while ((p
= strchr(p
, ':'))) {
1272 process_stat(struct perf_evsel
*counter
, int cpu
, int thread
, u64 tstamp
,
1273 struct perf_counts_values
*count
)
1275 PyObject
*handler
, *t
;
1276 static char handler_name
[256];
1279 t
= PyTuple_New(MAX_FIELDS
);
1281 Py_FatalError("couldn't create Python tuple");
1283 get_handler_name(handler_name
, sizeof(handler_name
),
1286 handler
= get_handler(handler_name
);
1288 pr_debug("can't find python handler %s\n", handler_name
);
1292 PyTuple_SetItem(t
, n
++, _PyLong_FromLong(cpu
));
1293 PyTuple_SetItem(t
, n
++, _PyLong_FromLong(thread
));
1295 tuple_set_u64(t
, n
++, tstamp
);
1296 tuple_set_u64(t
, n
++, count
->val
);
1297 tuple_set_u64(t
, n
++, count
->ena
);
1298 tuple_set_u64(t
, n
++, count
->run
);
1300 if (_PyTuple_Resize(&t
, n
) == -1)
1301 Py_FatalError("error resizing Python tuple");
1303 call_object(handler
, t
, handler_name
);
1308 static void python_process_stat(struct perf_stat_config
*config
,
1309 struct perf_evsel
*counter
, u64 tstamp
)
1311 struct thread_map
*threads
= counter
->threads
;
1312 struct cpu_map
*cpus
= counter
->cpus
;
1315 if (config
->aggr_mode
== AGGR_GLOBAL
) {
1316 process_stat(counter
, -1, -1, tstamp
,
1317 &counter
->counts
->aggr
);
1321 for (thread
= 0; thread
< threads
->nr
; thread
++) {
1322 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
1323 process_stat(counter
, cpus
->map
[cpu
],
1324 thread_map__pid(threads
, thread
), tstamp
,
1325 perf_counts(counter
->counts
, cpu
, thread
));
1330 static void python_process_stat_interval(u64 tstamp
)
1332 PyObject
*handler
, *t
;
1333 static const char handler_name
[] = "stat__interval";
1336 t
= PyTuple_New(MAX_FIELDS
);
1338 Py_FatalError("couldn't create Python tuple");
1340 handler
= get_handler(handler_name
);
1342 pr_debug("can't find python handler %s\n", handler_name
);
1346 tuple_set_u64(t
, n
++, tstamp
);
1348 if (_PyTuple_Resize(&t
, n
) == -1)
1349 Py_FatalError("error resizing Python tuple");
1351 call_object(handler
, t
, handler_name
);
1356 static int run_start_sub(void)
1358 main_module
= PyImport_AddModule("__main__");
1359 if (main_module
== NULL
)
1361 Py_INCREF(main_module
);
1363 main_dict
= PyModule_GetDict(main_module
);
1364 if (main_dict
== NULL
)
1366 Py_INCREF(main_dict
);
1368 try_call_object("trace_begin", NULL
);
1373 Py_XDECREF(main_dict
);
1374 Py_XDECREF(main_module
);
1378 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1379 tables->handler_name = get_handler(#table_name); \
1380 if (tables->handler_name) \
1381 tables->dbe.export_ ## name = python_export_ ## name; \
1384 #define SET_TABLE_HANDLER(name) \
1385 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1387 static void set_table_handlers(struct tables
*tables
)
1389 const char *perf_db_export_mode
= "perf_db_export_mode";
1390 const char *perf_db_export_calls
= "perf_db_export_calls";
1391 const char *perf_db_export_callchains
= "perf_db_export_callchains";
1392 PyObject
*db_export_mode
, *db_export_calls
, *db_export_callchains
;
1393 bool export_calls
= false;
1394 bool export_callchains
= false;
1397 memset(tables
, 0, sizeof(struct tables
));
1398 if (db_export__init(&tables
->dbe
))
1399 Py_FatalError("failed to initialize export");
1401 db_export_mode
= PyDict_GetItemString(main_dict
, perf_db_export_mode
);
1402 if (!db_export_mode
)
1405 ret
= PyObject_IsTrue(db_export_mode
);
1407 handler_call_die(perf_db_export_mode
);
1411 /* handle export calls */
1412 tables
->dbe
.crp
= NULL
;
1413 db_export_calls
= PyDict_GetItemString(main_dict
, perf_db_export_calls
);
1414 if (db_export_calls
) {
1415 ret
= PyObject_IsTrue(db_export_calls
);
1417 handler_call_die(perf_db_export_calls
);
1418 export_calls
= !!ret
;
1423 call_return_processor__new(python_process_call_return
,
1425 if (!tables
->dbe
.crp
)
1426 Py_FatalError("failed to create calls processor");
1429 /* handle export callchains */
1430 tables
->dbe
.cpr
= NULL
;
1431 db_export_callchains
= PyDict_GetItemString(main_dict
,
1432 perf_db_export_callchains
);
1433 if (db_export_callchains
) {
1434 ret
= PyObject_IsTrue(db_export_callchains
);
1436 handler_call_die(perf_db_export_callchains
);
1437 export_callchains
= !!ret
;
1440 if (export_callchains
) {
1442 * Attempt to use the call path root from the call return
1443 * processor, if the call return processor is in use. Otherwise,
1444 * we allocate a new call path root. This prevents exporting
1445 * duplicate call path ids when both are in use simultaniously.
1447 if (tables
->dbe
.crp
)
1448 tables
->dbe
.cpr
= tables
->dbe
.crp
->cpr
;
1450 tables
->dbe
.cpr
= call_path_root__new();
1452 if (!tables
->dbe
.cpr
)
1453 Py_FatalError("failed to create call path root");
1456 tables
->db_export_mode
= true;
1458 * Reserve per symbol space for symbol->db_id via symbol__priv()
1460 symbol_conf
.priv_size
= sizeof(u64
);
1462 SET_TABLE_HANDLER(evsel
);
1463 SET_TABLE_HANDLER(machine
);
1464 SET_TABLE_HANDLER(thread
);
1465 SET_TABLE_HANDLER(comm
);
1466 SET_TABLE_HANDLER(comm_thread
);
1467 SET_TABLE_HANDLER(dso
);
1468 SET_TABLE_HANDLER(symbol
);
1469 SET_TABLE_HANDLER(branch_type
);
1470 SET_TABLE_HANDLER(sample
);
1471 SET_TABLE_HANDLER(call_path
);
1472 SET_TABLE_HANDLER(call_return
);
1475 #if PY_MAJOR_VERSION < 3
1476 static void _free_command_line(const char **command_line
, int num
)
1481 static void _free_command_line(wchar_t **command_line
, int num
)
1484 for (i
= 0; i
< num
; i
++)
1485 PyMem_RawFree(command_line
[i
]);
1492 * Start trace script
1494 static int python_start_script(const char *script
, int argc
, const char **argv
)
1496 struct tables
*tables
= &tables_global
;
1497 #if PY_MAJOR_VERSION < 3
1498 const char **command_line
;
1500 wchar_t **command_line
;
1506 #if PY_MAJOR_VERSION < 3
1507 command_line
= malloc((argc
+ 1) * sizeof(const char *));
1508 command_line
[0] = script
;
1509 for (i
= 1; i
< argc
+ 1; i
++)
1510 command_line
[i
] = argv
[i
- 1];
1512 command_line
= malloc((argc
+ 1) * sizeof(wchar_t *));
1513 command_line
[0] = Py_DecodeLocale(script
, NULL
);
1514 for (i
= 1; i
< argc
+ 1; i
++)
1515 command_line
[i
] = Py_DecodeLocale(argv
[i
- 1], NULL
);
1520 #if PY_MAJOR_VERSION < 3
1521 initperf_trace_context();
1522 PySys_SetArgv(argc
+ 1, (char **)command_line
);
1524 PyInit_perf_trace_context();
1525 PySys_SetArgv(argc
+ 1, command_line
);
1528 fp
= fopen(script
, "r");
1530 sprintf(buf
, "Can't open python script \"%s\"", script
);
1536 err
= PyRun_SimpleFile(fp
, script
);
1538 fprintf(stderr
, "Error running python script %s\n", script
);
1542 err
= run_start_sub();
1544 fprintf(stderr
, "Error starting python script %s\n", script
);
1548 set_table_handlers(tables
);
1550 if (tables
->db_export_mode
) {
1551 err
= db_export__branch_types(&tables
->dbe
);
1556 _free_command_line(command_line
, argc
+ 1);
1561 _free_command_line(command_line
, argc
+ 1);
1566 static int python_flush_script(void)
1568 struct tables
*tables
= &tables_global
;
1570 return db_export__flush(&tables
->dbe
);
1576 static int python_stop_script(void)
1578 struct tables
*tables
= &tables_global
;
1580 try_call_object("trace_end", NULL
);
1582 db_export__exit(&tables
->dbe
);
1584 Py_XDECREF(main_dict
);
1585 Py_XDECREF(main_module
);
1591 static int python_generate_script(struct tep_handle
*pevent
, const char *outfile
)
1593 struct event_format
*event
= NULL
;
1594 struct format_field
*f
;
1595 char fname
[PATH_MAX
];
1596 int not_first
, count
;
1599 sprintf(fname
, "%s.py", outfile
);
1600 ofp
= fopen(fname
, "w");
1602 fprintf(stderr
, "couldn't open %s\n", fname
);
1605 fprintf(ofp
, "# perf script event handlers, "
1606 "generated by perf script -g python\n");
1608 fprintf(ofp
, "# Licensed under the terms of the GNU GPL"
1609 " License version 2\n\n");
1611 fprintf(ofp
, "# The common_* event handler fields are the most useful "
1612 "fields common to\n");
1614 fprintf(ofp
, "# all events. They don't necessarily correspond to "
1615 "the 'common_*' fields\n");
1617 fprintf(ofp
, "# in the format files. Those fields not available as "
1618 "handler params can\n");
1620 fprintf(ofp
, "# be retrieved using Python functions of the form "
1621 "common_*(context).\n");
1623 fprintf(ofp
, "# See the perf-script-python Documentation for the list "
1624 "of available functions.\n\n");
1626 fprintf(ofp
, "from __future__ import print_function\n\n");
1627 fprintf(ofp
, "import os\n");
1628 fprintf(ofp
, "import sys\n\n");
1630 fprintf(ofp
, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1631 fprintf(ofp
, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1632 fprintf(ofp
, "\nfrom perf_trace_context import *\n");
1633 fprintf(ofp
, "from Core import *\n\n\n");
1635 fprintf(ofp
, "def trace_begin():\n");
1636 fprintf(ofp
, "\tprint(\"in trace_begin\")\n\n");
1638 fprintf(ofp
, "def trace_end():\n");
1639 fprintf(ofp
, "\tprint(\"in trace_end\")\n\n");
1641 while ((event
= trace_find_next_event(pevent
, event
))) {
1642 fprintf(ofp
, "def %s__%s(", event
->system
, event
->name
);
1643 fprintf(ofp
, "event_name, ");
1644 fprintf(ofp
, "context, ");
1645 fprintf(ofp
, "common_cpu,\n");
1646 fprintf(ofp
, "\tcommon_secs, ");
1647 fprintf(ofp
, "common_nsecs, ");
1648 fprintf(ofp
, "common_pid, ");
1649 fprintf(ofp
, "common_comm,\n\t");
1650 fprintf(ofp
, "common_callchain, ");
1655 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1658 if (++count
% 5 == 0)
1659 fprintf(ofp
, "\n\t");
1661 fprintf(ofp
, "%s", f
->name
);
1665 if (++count
% 5 == 0)
1666 fprintf(ofp
, "\n\t\t");
1667 fprintf(ofp
, "perf_sample_dict");
1669 fprintf(ofp
, "):\n");
1671 fprintf(ofp
, "\t\tprint_header(event_name, common_cpu, "
1672 "common_secs, common_nsecs,\n\t\t\t"
1673 "common_pid, common_comm)\n\n");
1675 fprintf(ofp
, "\t\tprint(\"");
1680 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1683 if (count
&& count
% 3 == 0) {
1684 fprintf(ofp
, "\" \\\n\t\t\"");
1688 fprintf(ofp
, "%s=", f
->name
);
1689 if (f
->flags
& FIELD_IS_STRING
||
1690 f
->flags
& FIELD_IS_FLAG
||
1691 f
->flags
& FIELD_IS_ARRAY
||
1692 f
->flags
& FIELD_IS_SYMBOLIC
)
1693 fprintf(ofp
, "%%s");
1694 else if (f
->flags
& FIELD_IS_SIGNED
)
1695 fprintf(ofp
, "%%d");
1697 fprintf(ofp
, "%%u");
1700 fprintf(ofp
, "\" %% \\\n\t\t(");
1705 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1709 if (++count
% 5 == 0)
1710 fprintf(ofp
, "\n\t\t");
1712 if (f
->flags
& FIELD_IS_FLAG
) {
1713 if ((count
- 1) % 5 != 0) {
1714 fprintf(ofp
, "\n\t\t");
1717 fprintf(ofp
, "flag_str(\"");
1718 fprintf(ofp
, "%s__%s\", ", event
->system
,
1720 fprintf(ofp
, "\"%s\", %s)", f
->name
,
1722 } else if (f
->flags
& FIELD_IS_SYMBOLIC
) {
1723 if ((count
- 1) % 5 != 0) {
1724 fprintf(ofp
, "\n\t\t");
1727 fprintf(ofp
, "symbol_str(\"");
1728 fprintf(ofp
, "%s__%s\", ", event
->system
,
1730 fprintf(ofp
, "\"%s\", %s)", f
->name
,
1733 fprintf(ofp
, "%s", f
->name
);
1736 fprintf(ofp
, "))\n\n");
1738 fprintf(ofp
, "\t\tprint('Sample: {'+"
1739 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1741 fprintf(ofp
, "\t\tfor node in common_callchain:");
1742 fprintf(ofp
, "\n\t\t\tif 'sym' in node:");
1743 fprintf(ofp
, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))");
1744 fprintf(ofp
, "\n\t\t\telse:");
1745 fprintf(ofp
, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n");
1746 fprintf(ofp
, "\t\tprint()\n\n");
1750 fprintf(ofp
, "def trace_unhandled(event_name, context, "
1751 "event_fields_dict, perf_sample_dict):\n");
1753 fprintf(ofp
, "\t\tprint(get_dict_as_string(event_fields_dict))\n");
1754 fprintf(ofp
, "\t\tprint('Sample: {'+"
1755 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1757 fprintf(ofp
, "def print_header("
1758 "event_name, cpu, secs, nsecs, pid, comm):\n"
1759 "\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1760 "(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n");
1762 fprintf(ofp
, "def get_dict_as_string(a_dict, delimiter=' '):\n"
1763 "\treturn delimiter.join"
1764 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
1768 fprintf(stderr
, "generated Python script: %s\n", fname
);
1773 struct scripting_ops python_scripting_ops
= {
1775 .start_script
= python_start_script
,
1776 .flush_script
= python_flush_script
,
1777 .stop_script
= python_stop_script
,
1778 .process_event
= python_process_event
,
1779 .process_stat
= python_process_stat
,
1780 .process_stat_interval
= python_process_stat_interval
,
1781 .generate_script
= python_generate_script
,