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"
52 PyMODINIT_FUNC
initperf_trace_context(void);
54 #define TRACE_EVENT_TYPE_MAX \
55 ((1 << (sizeof(unsigned short) * 8)) - 1)
57 static DECLARE_BITMAP(events_defined
, TRACE_EVENT_TYPE_MAX
);
60 #define N_COMMON_FIELDS 7
62 extern struct scripting_context
*scripting_context
;
64 static char *cur_field_name
;
65 static int zero_flag_atom
;
67 static PyObject
*main_module
, *main_dict
;
71 PyObject
*evsel_handler
;
72 PyObject
*machine_handler
;
73 PyObject
*thread_handler
;
74 PyObject
*comm_handler
;
75 PyObject
*comm_thread_handler
;
76 PyObject
*dso_handler
;
77 PyObject
*symbol_handler
;
78 PyObject
*branch_type_handler
;
79 PyObject
*sample_handler
;
80 PyObject
*call_path_handler
;
81 PyObject
*call_return_handler
;
85 static struct tables tables_global
;
87 static void handler_call_die(const char *handler_name
) __noreturn
;
88 static void handler_call_die(const char *handler_name
)
91 Py_FatalError("problem in Python trace event handler");
92 // Py_FatalError does not return
93 // but we have to make the compiler happy
98 * Insert val into into the dictionary and decrement the reference counter.
99 * This is necessary for dictionaries since PyDict_SetItemString() does not
100 * steal a reference, as opposed to PyTuple_SetItem().
102 static void pydict_set_item_string_decref(PyObject
*dict
, const char *key
, PyObject
*val
)
104 PyDict_SetItemString(dict
, key
, val
);
108 static PyObject
*get_handler(const char *handler_name
)
112 handler
= PyDict_GetItemString(main_dict
, handler_name
);
113 if (handler
&& !PyCallable_Check(handler
))
118 static int get_argument_count(PyObject
*handler
)
123 * The attribute for the code object is func_code in Python 2,
124 * whereas it is __code__ in Python 3.0+.
126 PyObject
*code_obj
= PyObject_GetAttrString(handler
,
128 if (PyErr_Occurred()) {
130 code_obj
= PyObject_GetAttrString(handler
,
135 PyObject
*arg_count_obj
= PyObject_GetAttrString(code_obj
,
138 arg_count
= (int) PyInt_AsLong(arg_count_obj
);
139 Py_DECREF(arg_count_obj
);
146 static void call_object(PyObject
*handler
, PyObject
*args
, const char *die_msg
)
150 retval
= PyObject_CallObject(handler
, args
);
152 handler_call_die(die_msg
);
156 static void try_call_object(const char *handler_name
, PyObject
*args
)
160 handler
= get_handler(handler_name
);
162 call_object(handler
, args
, handler_name
);
165 static void define_value(enum print_arg_type field_type
,
167 const char *field_name
,
168 const char *field_value
,
169 const char *field_str
)
171 const char *handler_name
= "define_flag_value";
173 unsigned long long value
;
176 if (field_type
== PRINT_SYMBOL
)
177 handler_name
= "define_symbolic_value";
181 Py_FatalError("couldn't create Python tuple");
183 value
= eval_flag(field_value
);
185 PyTuple_SetItem(t
, n
++, PyString_FromString(ev_name
));
186 PyTuple_SetItem(t
, n
++, PyString_FromString(field_name
));
187 PyTuple_SetItem(t
, n
++, PyInt_FromLong(value
));
188 PyTuple_SetItem(t
, n
++, PyString_FromString(field_str
));
190 try_call_object(handler_name
, t
);
195 static void define_values(enum print_arg_type field_type
,
196 struct print_flag_sym
*field
,
198 const char *field_name
)
200 define_value(field_type
, ev_name
, field_name
, field
->value
,
204 define_values(field_type
, field
->next
, ev_name
, field_name
);
207 static void define_field(enum print_arg_type field_type
,
209 const char *field_name
,
212 const char *handler_name
= "define_flag_field";
216 if (field_type
== PRINT_SYMBOL
)
217 handler_name
= "define_symbolic_field";
219 if (field_type
== PRINT_FLAGS
)
224 Py_FatalError("couldn't create Python tuple");
226 PyTuple_SetItem(t
, n
++, PyString_FromString(ev_name
));
227 PyTuple_SetItem(t
, n
++, PyString_FromString(field_name
));
228 if (field_type
== PRINT_FLAGS
)
229 PyTuple_SetItem(t
, n
++, PyString_FromString(delim
));
231 try_call_object(handler_name
, t
);
236 static void define_event_symbols(struct event_format
*event
,
238 struct print_arg
*args
)
243 switch (args
->type
) {
247 define_value(PRINT_FLAGS
, ev_name
, cur_field_name
, "0",
252 free(cur_field_name
);
253 cur_field_name
= strdup(args
->field
.name
);
256 define_event_symbols(event
, ev_name
, args
->flags
.field
);
257 define_field(PRINT_FLAGS
, ev_name
, cur_field_name
,
259 define_values(PRINT_FLAGS
, args
->flags
.flags
, ev_name
,
263 define_event_symbols(event
, ev_name
, args
->symbol
.field
);
264 define_field(PRINT_SYMBOL
, ev_name
, cur_field_name
, NULL
);
265 define_values(PRINT_SYMBOL
, args
->symbol
.symbols
, ev_name
,
270 define_event_symbols(event
, ev_name
, args
->hex
.field
);
271 define_event_symbols(event
, ev_name
, args
->hex
.size
);
273 case PRINT_INT_ARRAY
:
274 define_event_symbols(event
, ev_name
, args
->int_array
.field
);
275 define_event_symbols(event
, ev_name
, args
->int_array
.count
);
276 define_event_symbols(event
, ev_name
, args
->int_array
.el_size
);
281 define_event_symbols(event
, ev_name
, args
->typecast
.item
);
284 if (strcmp(args
->op
.op
, ":") == 0)
286 define_event_symbols(event
, ev_name
, args
->op
.left
);
287 define_event_symbols(event
, ev_name
, args
->op
.right
);
290 /* gcc warns for these? */
292 case PRINT_DYNAMIC_ARRAY
:
293 case PRINT_DYNAMIC_ARRAY_LEN
:
296 /* we should warn... */
301 define_event_symbols(event
, ev_name
, args
->next
);
304 static PyObject
*get_field_numeric_entry(struct event_format
*event
,
305 struct format_field
*field
, void *data
)
307 bool is_array
= field
->flags
& FIELD_IS_ARRAY
;
308 PyObject
*obj
= NULL
, *list
= NULL
;
309 unsigned long long val
;
310 unsigned int item_size
, n_items
, i
;
313 list
= PyList_New(field
->arraylen
);
314 item_size
= field
->size
/ field
->arraylen
;
315 n_items
= field
->arraylen
;
317 item_size
= field
->size
;
321 for (i
= 0; i
< n_items
; i
++) {
323 val
= read_size(event
, data
+ field
->offset
+ i
* item_size
,
325 if (field
->flags
& FIELD_IS_SIGNED
) {
326 if ((long long)val
>= LONG_MIN
&&
327 (long long)val
<= LONG_MAX
)
328 obj
= PyInt_FromLong(val
);
330 obj
= PyLong_FromLongLong(val
);
333 obj
= PyInt_FromLong(val
);
335 obj
= PyLong_FromUnsignedLongLong(val
);
338 PyList_SET_ITEM(list
, i
, obj
);
346 static PyObject
*python_process_callchain(struct perf_sample
*sample
,
347 struct perf_evsel
*evsel
,
348 struct addr_location
*al
)
352 pylist
= PyList_New(0);
354 Py_FatalError("couldn't create Python list");
356 if (!symbol_conf
.use_callchain
|| !sample
->callchain
)
359 if (thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
361 scripting_max_stack
) != 0) {
362 pr_err("Failed to resolve callchain. Skipping\n");
365 callchain_cursor_commit(&callchain_cursor
);
370 struct callchain_cursor_node
*node
;
371 node
= callchain_cursor_current(&callchain_cursor
);
375 pyelem
= PyDict_New();
377 Py_FatalError("couldn't create Python dictionary");
380 pydict_set_item_string_decref(pyelem
, "ip",
381 PyLong_FromUnsignedLongLong(node
->ip
));
384 PyObject
*pysym
= PyDict_New();
386 Py_FatalError("couldn't create Python dictionary");
387 pydict_set_item_string_decref(pysym
, "start",
388 PyLong_FromUnsignedLongLong(node
->sym
->start
));
389 pydict_set_item_string_decref(pysym
, "end",
390 PyLong_FromUnsignedLongLong(node
->sym
->end
));
391 pydict_set_item_string_decref(pysym
, "binding",
392 PyInt_FromLong(node
->sym
->binding
));
393 pydict_set_item_string_decref(pysym
, "name",
394 PyString_FromStringAndSize(node
->sym
->name
,
395 node
->sym
->namelen
));
396 pydict_set_item_string_decref(pyelem
, "sym", pysym
);
400 struct map
*map
= node
->map
;
401 const char *dsoname
= "[unknown]";
402 if (map
&& map
->dso
) {
403 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
404 dsoname
= map
->dso
->long_name
;
406 dsoname
= map
->dso
->name
;
408 pydict_set_item_string_decref(pyelem
, "dso",
409 PyString_FromString(dsoname
));
412 callchain_cursor_advance(&callchain_cursor
);
413 PyList_Append(pylist
, pyelem
);
421 static PyObject
*get_sample_value_as_tuple(struct sample_read_value
*value
)
427 Py_FatalError("couldn't create Python tuple");
428 PyTuple_SetItem(t
, 0, PyLong_FromUnsignedLongLong(value
->id
));
429 PyTuple_SetItem(t
, 1, PyLong_FromUnsignedLongLong(value
->value
));
433 static void set_sample_read_in_dict(PyObject
*dict_sample
,
434 struct perf_sample
*sample
,
435 struct perf_evsel
*evsel
)
437 u64 read_format
= evsel
->attr
.read_format
;
441 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
442 pydict_set_item_string_decref(dict_sample
, "time_enabled",
443 PyLong_FromUnsignedLongLong(sample
->read
.time_enabled
));
446 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
447 pydict_set_item_string_decref(dict_sample
, "time_running",
448 PyLong_FromUnsignedLongLong(sample
->read
.time_running
));
451 if (read_format
& PERF_FORMAT_GROUP
)
452 values
= PyList_New(sample
->read
.group
.nr
);
454 values
= PyList_New(1);
457 Py_FatalError("couldn't create Python list");
459 if (read_format
& PERF_FORMAT_GROUP
) {
460 for (i
= 0; i
< sample
->read
.group
.nr
; i
++) {
461 PyObject
*t
= get_sample_value_as_tuple(&sample
->read
.group
.values
[i
]);
462 PyList_SET_ITEM(values
, i
, t
);
465 PyObject
*t
= get_sample_value_as_tuple(&sample
->read
.one
);
466 PyList_SET_ITEM(values
, 0, t
);
468 pydict_set_item_string_decref(dict_sample
, "values", values
);
471 static PyObject
*get_perf_sample_dict(struct perf_sample
*sample
,
472 struct perf_evsel
*evsel
,
473 struct addr_location
*al
,
476 PyObject
*dict
, *dict_sample
;
480 Py_FatalError("couldn't create Python dictionary");
482 dict_sample
= PyDict_New();
484 Py_FatalError("couldn't create Python dictionary");
486 pydict_set_item_string_decref(dict
, "ev_name", PyString_FromString(perf_evsel__name(evsel
)));
487 pydict_set_item_string_decref(dict
, "attr", PyString_FromStringAndSize(
488 (const char *)&evsel
->attr
, sizeof(evsel
->attr
)));
490 pydict_set_item_string_decref(dict_sample
, "pid",
491 PyInt_FromLong(sample
->pid
));
492 pydict_set_item_string_decref(dict_sample
, "tid",
493 PyInt_FromLong(sample
->tid
));
494 pydict_set_item_string_decref(dict_sample
, "cpu",
495 PyInt_FromLong(sample
->cpu
));
496 pydict_set_item_string_decref(dict_sample
, "ip",
497 PyLong_FromUnsignedLongLong(sample
->ip
));
498 pydict_set_item_string_decref(dict_sample
, "time",
499 PyLong_FromUnsignedLongLong(sample
->time
));
500 pydict_set_item_string_decref(dict_sample
, "period",
501 PyLong_FromUnsignedLongLong(sample
->period
));
502 pydict_set_item_string_decref(dict_sample
, "phys_addr",
503 PyLong_FromUnsignedLongLong(sample
->phys_addr
));
504 set_sample_read_in_dict(dict_sample
, sample
, evsel
);
505 pydict_set_item_string_decref(dict
, "sample", dict_sample
);
507 pydict_set_item_string_decref(dict
, "raw_buf", PyString_FromStringAndSize(
508 (const char *)sample
->raw_data
, sample
->raw_size
));
509 pydict_set_item_string_decref(dict
, "comm",
510 PyString_FromString(thread__comm_str(al
->thread
)));
512 pydict_set_item_string_decref(dict
, "dso",
513 PyString_FromString(al
->map
->dso
->name
));
516 pydict_set_item_string_decref(dict
, "symbol",
517 PyString_FromString(al
->sym
->name
));
520 pydict_set_item_string_decref(dict
, "callchain", callchain
);
525 static void python_process_tracepoint(struct perf_sample
*sample
,
526 struct perf_evsel
*evsel
,
527 struct addr_location
*al
)
529 struct event_format
*event
= evsel
->tp_format
;
530 PyObject
*handler
, *context
, *t
, *obj
= NULL
, *callchain
;
531 PyObject
*dict
= NULL
, *all_entries_dict
= NULL
;
532 static char handler_name
[256];
533 struct format_field
*field
;
537 int cpu
= sample
->cpu
;
538 void *data
= sample
->raw_data
;
539 unsigned long long nsecs
= sample
->time
;
540 const char *comm
= thread__comm_str(al
->thread
);
541 const char *default_handler_name
= "trace_unhandled";
544 snprintf(handler_name
, sizeof(handler_name
),
545 "ug! no event found for type %" PRIu64
, (u64
)evsel
->attr
.config
);
546 Py_FatalError(handler_name
);
549 pid
= raw_field_value(event
, "common_pid", data
);
551 sprintf(handler_name
, "%s__%s", event
->system
, event
->name
);
553 if (!test_and_set_bit(event
->id
, events_defined
))
554 define_event_symbols(event
, handler_name
, event
->print_fmt
.args
);
556 handler
= get_handler(handler_name
);
558 handler
= get_handler(default_handler_name
);
563 Py_FatalError("couldn't create Python dict");
566 t
= PyTuple_New(MAX_FIELDS
);
568 Py_FatalError("couldn't create Python tuple");
571 s
= nsecs
/ NSEC_PER_SEC
;
572 ns
= nsecs
- s
* NSEC_PER_SEC
;
574 scripting_context
->event_data
= data
;
575 scripting_context
->pevent
= evsel
->tp_format
->pevent
;
577 context
= PyCObject_FromVoidPtr(scripting_context
, NULL
);
579 PyTuple_SetItem(t
, n
++, PyString_FromString(handler_name
));
580 PyTuple_SetItem(t
, n
++, context
);
583 callchain
= python_process_callchain(sample
, evsel
, al
);
584 /* Need an additional reference for the perf_sample dict */
585 Py_INCREF(callchain
);
588 PyTuple_SetItem(t
, n
++, PyInt_FromLong(cpu
));
589 PyTuple_SetItem(t
, n
++, PyInt_FromLong(s
));
590 PyTuple_SetItem(t
, n
++, PyInt_FromLong(ns
));
591 PyTuple_SetItem(t
, n
++, PyInt_FromLong(pid
));
592 PyTuple_SetItem(t
, n
++, PyString_FromString(comm
));
593 PyTuple_SetItem(t
, n
++, callchain
);
595 pydict_set_item_string_decref(dict
, "common_cpu", PyInt_FromLong(cpu
));
596 pydict_set_item_string_decref(dict
, "common_s", PyInt_FromLong(s
));
597 pydict_set_item_string_decref(dict
, "common_ns", PyInt_FromLong(ns
));
598 pydict_set_item_string_decref(dict
, "common_pid", PyInt_FromLong(pid
));
599 pydict_set_item_string_decref(dict
, "common_comm", PyString_FromString(comm
));
600 pydict_set_item_string_decref(dict
, "common_callchain", callchain
);
602 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
603 unsigned int offset
, len
;
604 unsigned long long val
;
606 if (field
->flags
& FIELD_IS_ARRAY
) {
607 offset
= field
->offset
;
609 if (field
->flags
& FIELD_IS_DYNAMIC
) {
610 val
= pevent_read_number(scripting_context
->pevent
,
616 if (field
->flags
& FIELD_IS_STRING
&&
617 is_printable_array(data
+ offset
, len
)) {
618 obj
= PyString_FromString((char *) data
+ offset
);
620 obj
= PyByteArray_FromStringAndSize((const char *) data
+ offset
, len
);
621 field
->flags
&= ~FIELD_IS_STRING
;
623 } else { /* FIELD_IS_NUMERIC */
624 obj
= get_field_numeric_entry(event
, field
, data
);
627 PyTuple_SetItem(t
, n
++, obj
);
629 pydict_set_item_string_decref(dict
, field
->name
, obj
);
634 PyTuple_SetItem(t
, n
++, dict
);
636 if (get_argument_count(handler
) == (int) n
+ 1) {
637 all_entries_dict
= get_perf_sample_dict(sample
, evsel
, al
,
639 PyTuple_SetItem(t
, n
++, all_entries_dict
);
641 Py_DECREF(callchain
);
644 if (_PyTuple_Resize(&t
, n
) == -1)
645 Py_FatalError("error resizing Python tuple");
648 call_object(handler
, t
, handler_name
);
650 call_object(handler
, t
, default_handler_name
);
654 Py_XDECREF(all_entries_dict
);
658 static PyObject
*tuple_new(unsigned int sz
)
664 Py_FatalError("couldn't create Python tuple");
668 static int tuple_set_u64(PyObject
*t
, unsigned int pos
, u64 val
)
670 #if BITS_PER_LONG == 64
671 return PyTuple_SetItem(t
, pos
, PyInt_FromLong(val
));
673 #if BITS_PER_LONG == 32
674 return PyTuple_SetItem(t
, pos
, PyLong_FromLongLong(val
));
678 static int tuple_set_s32(PyObject
*t
, unsigned int pos
, s32 val
)
680 return PyTuple_SetItem(t
, pos
, PyInt_FromLong(val
));
683 static int tuple_set_string(PyObject
*t
, unsigned int pos
, const char *s
)
685 return PyTuple_SetItem(t
, pos
, PyString_FromString(s
));
688 static int python_export_evsel(struct db_export
*dbe
, struct perf_evsel
*evsel
)
690 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
695 tuple_set_u64(t
, 0, evsel
->db_id
);
696 tuple_set_string(t
, 1, perf_evsel__name(evsel
));
698 call_object(tables
->evsel_handler
, t
, "evsel_table");
705 static int python_export_machine(struct db_export
*dbe
,
706 struct machine
*machine
)
708 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
713 tuple_set_u64(t
, 0, machine
->db_id
);
714 tuple_set_s32(t
, 1, machine
->pid
);
715 tuple_set_string(t
, 2, machine
->root_dir
? machine
->root_dir
: "");
717 call_object(tables
->machine_handler
, t
, "machine_table");
724 static int python_export_thread(struct db_export
*dbe
, struct thread
*thread
,
725 u64 main_thread_db_id
, struct machine
*machine
)
727 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
732 tuple_set_u64(t
, 0, thread
->db_id
);
733 tuple_set_u64(t
, 1, machine
->db_id
);
734 tuple_set_u64(t
, 2, main_thread_db_id
);
735 tuple_set_s32(t
, 3, thread
->pid_
);
736 tuple_set_s32(t
, 4, thread
->tid
);
738 call_object(tables
->thread_handler
, t
, "thread_table");
745 static int python_export_comm(struct db_export
*dbe
, struct comm
*comm
)
747 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
752 tuple_set_u64(t
, 0, comm
->db_id
);
753 tuple_set_string(t
, 1, comm__str(comm
));
755 call_object(tables
->comm_handler
, t
, "comm_table");
762 static int python_export_comm_thread(struct db_export
*dbe
, u64 db_id
,
763 struct comm
*comm
, struct thread
*thread
)
765 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
770 tuple_set_u64(t
, 0, db_id
);
771 tuple_set_u64(t
, 1, comm
->db_id
);
772 tuple_set_u64(t
, 2, thread
->db_id
);
774 call_object(tables
->comm_thread_handler
, t
, "comm_thread_table");
781 static int python_export_dso(struct db_export
*dbe
, struct dso
*dso
,
782 struct machine
*machine
)
784 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
785 char sbuild_id
[SBUILD_ID_SIZE
];
788 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
792 tuple_set_u64(t
, 0, dso
->db_id
);
793 tuple_set_u64(t
, 1, machine
->db_id
);
794 tuple_set_string(t
, 2, dso
->short_name
);
795 tuple_set_string(t
, 3, dso
->long_name
);
796 tuple_set_string(t
, 4, sbuild_id
);
798 call_object(tables
->dso_handler
, t
, "dso_table");
805 static int python_export_symbol(struct db_export
*dbe
, struct symbol
*sym
,
808 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
809 u64
*sym_db_id
= symbol__priv(sym
);
814 tuple_set_u64(t
, 0, *sym_db_id
);
815 tuple_set_u64(t
, 1, dso
->db_id
);
816 tuple_set_u64(t
, 2, sym
->start
);
817 tuple_set_u64(t
, 3, sym
->end
);
818 tuple_set_s32(t
, 4, sym
->binding
);
819 tuple_set_string(t
, 5, sym
->name
);
821 call_object(tables
->symbol_handler
, t
, "symbol_table");
828 static int python_export_branch_type(struct db_export
*dbe
, u32 branch_type
,
831 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
836 tuple_set_s32(t
, 0, branch_type
);
837 tuple_set_string(t
, 1, name
);
839 call_object(tables
->branch_type_handler
, t
, "branch_type_table");
846 static int python_export_sample(struct db_export
*dbe
,
847 struct export_sample
*es
)
849 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
854 tuple_set_u64(t
, 0, es
->db_id
);
855 tuple_set_u64(t
, 1, es
->evsel
->db_id
);
856 tuple_set_u64(t
, 2, es
->al
->machine
->db_id
);
857 tuple_set_u64(t
, 3, es
->al
->thread
->db_id
);
858 tuple_set_u64(t
, 4, es
->comm_db_id
);
859 tuple_set_u64(t
, 5, es
->dso_db_id
);
860 tuple_set_u64(t
, 6, es
->sym_db_id
);
861 tuple_set_u64(t
, 7, es
->offset
);
862 tuple_set_u64(t
, 8, es
->sample
->ip
);
863 tuple_set_u64(t
, 9, es
->sample
->time
);
864 tuple_set_s32(t
, 10, es
->sample
->cpu
);
865 tuple_set_u64(t
, 11, es
->addr_dso_db_id
);
866 tuple_set_u64(t
, 12, es
->addr_sym_db_id
);
867 tuple_set_u64(t
, 13, es
->addr_offset
);
868 tuple_set_u64(t
, 14, es
->sample
->addr
);
869 tuple_set_u64(t
, 15, es
->sample
->period
);
870 tuple_set_u64(t
, 16, es
->sample
->weight
);
871 tuple_set_u64(t
, 17, es
->sample
->transaction
);
872 tuple_set_u64(t
, 18, es
->sample
->data_src
);
873 tuple_set_s32(t
, 19, es
->sample
->flags
& PERF_BRANCH_MASK
);
874 tuple_set_s32(t
, 20, !!(es
->sample
->flags
& PERF_IP_FLAG_IN_TX
));
875 tuple_set_u64(t
, 21, es
->call_path_id
);
877 call_object(tables
->sample_handler
, t
, "sample_table");
884 static int python_export_call_path(struct db_export
*dbe
, struct call_path
*cp
)
886 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
888 u64 parent_db_id
, sym_db_id
;
890 parent_db_id
= cp
->parent
? cp
->parent
->db_id
: 0;
891 sym_db_id
= cp
->sym
? *(u64
*)symbol__priv(cp
->sym
) : 0;
895 tuple_set_u64(t
, 0, cp
->db_id
);
896 tuple_set_u64(t
, 1, parent_db_id
);
897 tuple_set_u64(t
, 2, sym_db_id
);
898 tuple_set_u64(t
, 3, cp
->ip
);
900 call_object(tables
->call_path_handler
, t
, "call_path_table");
907 static int python_export_call_return(struct db_export
*dbe
,
908 struct call_return
*cr
)
910 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
911 u64 comm_db_id
= cr
->comm
? cr
->comm
->db_id
: 0;
916 tuple_set_u64(t
, 0, cr
->db_id
);
917 tuple_set_u64(t
, 1, cr
->thread
->db_id
);
918 tuple_set_u64(t
, 2, comm_db_id
);
919 tuple_set_u64(t
, 3, cr
->cp
->db_id
);
920 tuple_set_u64(t
, 4, cr
->call_time
);
921 tuple_set_u64(t
, 5, cr
->return_time
);
922 tuple_set_u64(t
, 6, cr
->branch_count
);
923 tuple_set_u64(t
, 7, cr
->call_ref
);
924 tuple_set_u64(t
, 8, cr
->return_ref
);
925 tuple_set_u64(t
, 9, cr
->cp
->parent
->db_id
);
926 tuple_set_s32(t
, 10, cr
->flags
);
928 call_object(tables
->call_return_handler
, t
, "call_return_table");
935 static int python_process_call_return(struct call_return
*cr
, void *data
)
937 struct db_export
*dbe
= data
;
939 return db_export__call_return(dbe
, cr
);
942 static void python_process_general_event(struct perf_sample
*sample
,
943 struct perf_evsel
*evsel
,
944 struct addr_location
*al
)
946 PyObject
*handler
, *t
, *dict
, *callchain
;
947 static char handler_name
[64];
950 snprintf(handler_name
, sizeof(handler_name
), "%s", "process_event");
952 handler
= get_handler(handler_name
);
957 * Use the MAX_FIELDS to make the function expandable, though
958 * currently there is only one item for the tuple.
960 t
= PyTuple_New(MAX_FIELDS
);
962 Py_FatalError("couldn't create Python tuple");
965 callchain
= python_process_callchain(sample
, evsel
, al
);
966 dict
= get_perf_sample_dict(sample
, evsel
, al
, callchain
);
968 PyTuple_SetItem(t
, n
++, dict
);
969 if (_PyTuple_Resize(&t
, n
) == -1)
970 Py_FatalError("error resizing Python tuple");
972 call_object(handler
, t
, handler_name
);
978 static void python_process_event(union perf_event
*event
,
979 struct perf_sample
*sample
,
980 struct perf_evsel
*evsel
,
981 struct addr_location
*al
)
983 struct tables
*tables
= &tables_global
;
985 switch (evsel
->attr
.type
) {
986 case PERF_TYPE_TRACEPOINT
:
987 python_process_tracepoint(sample
, evsel
, al
);
989 /* Reserve for future process_hw/sw/raw APIs */
991 if (tables
->db_export_mode
)
992 db_export__sample(&tables
->dbe
, event
, sample
, evsel
, al
);
994 python_process_general_event(sample
, evsel
, al
);
998 static void get_handler_name(char *str
, size_t size
,
999 struct perf_evsel
*evsel
)
1003 scnprintf(str
, size
, "stat__%s", perf_evsel__name(evsel
));
1005 while ((p
= strchr(p
, ':'))) {
1012 process_stat(struct perf_evsel
*counter
, int cpu
, int thread
, u64 tstamp
,
1013 struct perf_counts_values
*count
)
1015 PyObject
*handler
, *t
;
1016 static char handler_name
[256];
1019 t
= PyTuple_New(MAX_FIELDS
);
1021 Py_FatalError("couldn't create Python tuple");
1023 get_handler_name(handler_name
, sizeof(handler_name
),
1026 handler
= get_handler(handler_name
);
1028 pr_debug("can't find python handler %s\n", handler_name
);
1032 PyTuple_SetItem(t
, n
++, PyInt_FromLong(cpu
));
1033 PyTuple_SetItem(t
, n
++, PyInt_FromLong(thread
));
1035 tuple_set_u64(t
, n
++, tstamp
);
1036 tuple_set_u64(t
, n
++, count
->val
);
1037 tuple_set_u64(t
, n
++, count
->ena
);
1038 tuple_set_u64(t
, n
++, count
->run
);
1040 if (_PyTuple_Resize(&t
, n
) == -1)
1041 Py_FatalError("error resizing Python tuple");
1043 call_object(handler
, t
, handler_name
);
1048 static void python_process_stat(struct perf_stat_config
*config
,
1049 struct perf_evsel
*counter
, u64 tstamp
)
1051 struct thread_map
*threads
= counter
->threads
;
1052 struct cpu_map
*cpus
= counter
->cpus
;
1055 if (config
->aggr_mode
== AGGR_GLOBAL
) {
1056 process_stat(counter
, -1, -1, tstamp
,
1057 &counter
->counts
->aggr
);
1061 for (thread
= 0; thread
< threads
->nr
; thread
++) {
1062 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
1063 process_stat(counter
, cpus
->map
[cpu
],
1064 thread_map__pid(threads
, thread
), tstamp
,
1065 perf_counts(counter
->counts
, cpu
, thread
));
1070 static void python_process_stat_interval(u64 tstamp
)
1072 PyObject
*handler
, *t
;
1073 static const char handler_name
[] = "stat__interval";
1076 t
= PyTuple_New(MAX_FIELDS
);
1078 Py_FatalError("couldn't create Python tuple");
1080 handler
= get_handler(handler_name
);
1082 pr_debug("can't find python handler %s\n", handler_name
);
1086 tuple_set_u64(t
, n
++, tstamp
);
1088 if (_PyTuple_Resize(&t
, n
) == -1)
1089 Py_FatalError("error resizing Python tuple");
1091 call_object(handler
, t
, handler_name
);
1096 static int run_start_sub(void)
1098 main_module
= PyImport_AddModule("__main__");
1099 if (main_module
== NULL
)
1101 Py_INCREF(main_module
);
1103 main_dict
= PyModule_GetDict(main_module
);
1104 if (main_dict
== NULL
)
1106 Py_INCREF(main_dict
);
1108 try_call_object("trace_begin", NULL
);
1113 Py_XDECREF(main_dict
);
1114 Py_XDECREF(main_module
);
1118 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1119 tables->handler_name = get_handler(#table_name); \
1120 if (tables->handler_name) \
1121 tables->dbe.export_ ## name = python_export_ ## name; \
1124 #define SET_TABLE_HANDLER(name) \
1125 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1127 static void set_table_handlers(struct tables
*tables
)
1129 const char *perf_db_export_mode
= "perf_db_export_mode";
1130 const char *perf_db_export_calls
= "perf_db_export_calls";
1131 const char *perf_db_export_callchains
= "perf_db_export_callchains";
1132 PyObject
*db_export_mode
, *db_export_calls
, *db_export_callchains
;
1133 bool export_calls
= false;
1134 bool export_callchains
= false;
1137 memset(tables
, 0, sizeof(struct tables
));
1138 if (db_export__init(&tables
->dbe
))
1139 Py_FatalError("failed to initialize export");
1141 db_export_mode
= PyDict_GetItemString(main_dict
, perf_db_export_mode
);
1142 if (!db_export_mode
)
1145 ret
= PyObject_IsTrue(db_export_mode
);
1147 handler_call_die(perf_db_export_mode
);
1151 /* handle export calls */
1152 tables
->dbe
.crp
= NULL
;
1153 db_export_calls
= PyDict_GetItemString(main_dict
, perf_db_export_calls
);
1154 if (db_export_calls
) {
1155 ret
= PyObject_IsTrue(db_export_calls
);
1157 handler_call_die(perf_db_export_calls
);
1158 export_calls
= !!ret
;
1163 call_return_processor__new(python_process_call_return
,
1165 if (!tables
->dbe
.crp
)
1166 Py_FatalError("failed to create calls processor");
1169 /* handle export callchains */
1170 tables
->dbe
.cpr
= NULL
;
1171 db_export_callchains
= PyDict_GetItemString(main_dict
,
1172 perf_db_export_callchains
);
1173 if (db_export_callchains
) {
1174 ret
= PyObject_IsTrue(db_export_callchains
);
1176 handler_call_die(perf_db_export_callchains
);
1177 export_callchains
= !!ret
;
1180 if (export_callchains
) {
1182 * Attempt to use the call path root from the call return
1183 * processor, if the call return processor is in use. Otherwise,
1184 * we allocate a new call path root. This prevents exporting
1185 * duplicate call path ids when both are in use simultaniously.
1187 if (tables
->dbe
.crp
)
1188 tables
->dbe
.cpr
= tables
->dbe
.crp
->cpr
;
1190 tables
->dbe
.cpr
= call_path_root__new();
1192 if (!tables
->dbe
.cpr
)
1193 Py_FatalError("failed to create call path root");
1196 tables
->db_export_mode
= true;
1198 * Reserve per symbol space for symbol->db_id via symbol__priv()
1200 symbol_conf
.priv_size
= sizeof(u64
);
1202 SET_TABLE_HANDLER(evsel
);
1203 SET_TABLE_HANDLER(machine
);
1204 SET_TABLE_HANDLER(thread
);
1205 SET_TABLE_HANDLER(comm
);
1206 SET_TABLE_HANDLER(comm_thread
);
1207 SET_TABLE_HANDLER(dso
);
1208 SET_TABLE_HANDLER(symbol
);
1209 SET_TABLE_HANDLER(branch_type
);
1210 SET_TABLE_HANDLER(sample
);
1211 SET_TABLE_HANDLER(call_path
);
1212 SET_TABLE_HANDLER(call_return
);
1216 * Start trace script
1218 static int python_start_script(const char *script
, int argc
, const char **argv
)
1220 struct tables
*tables
= &tables_global
;
1221 const char **command_line
;
1226 command_line
= malloc((argc
+ 1) * sizeof(const char *));
1227 command_line
[0] = script
;
1228 for (i
= 1; i
< argc
+ 1; i
++)
1229 command_line
[i
] = argv
[i
- 1];
1233 initperf_trace_context();
1235 PySys_SetArgv(argc
+ 1, (char **)command_line
);
1237 fp
= fopen(script
, "r");
1239 sprintf(buf
, "Can't open python script \"%s\"", script
);
1245 err
= PyRun_SimpleFile(fp
, script
);
1247 fprintf(stderr
, "Error running python script %s\n", script
);
1251 err
= run_start_sub();
1253 fprintf(stderr
, "Error starting python script %s\n", script
);
1257 set_table_handlers(tables
);
1259 if (tables
->db_export_mode
) {
1260 err
= db_export__branch_types(&tables
->dbe
);
1275 static int python_flush_script(void)
1277 struct tables
*tables
= &tables_global
;
1279 return db_export__flush(&tables
->dbe
);
1285 static int python_stop_script(void)
1287 struct tables
*tables
= &tables_global
;
1289 try_call_object("trace_end", NULL
);
1291 db_export__exit(&tables
->dbe
);
1293 Py_XDECREF(main_dict
);
1294 Py_XDECREF(main_module
);
1300 static int python_generate_script(struct pevent
*pevent
, const char *outfile
)
1302 struct event_format
*event
= NULL
;
1303 struct format_field
*f
;
1304 char fname
[PATH_MAX
];
1305 int not_first
, count
;
1308 sprintf(fname
, "%s.py", outfile
);
1309 ofp
= fopen(fname
, "w");
1311 fprintf(stderr
, "couldn't open %s\n", fname
);
1314 fprintf(ofp
, "# perf script event handlers, "
1315 "generated by perf script -g python\n");
1317 fprintf(ofp
, "# Licensed under the terms of the GNU GPL"
1318 " License version 2\n\n");
1320 fprintf(ofp
, "# The common_* event handler fields are the most useful "
1321 "fields common to\n");
1323 fprintf(ofp
, "# all events. They don't necessarily correspond to "
1324 "the 'common_*' fields\n");
1326 fprintf(ofp
, "# in the format files. Those fields not available as "
1327 "handler params can\n");
1329 fprintf(ofp
, "# be retrieved using Python functions of the form "
1330 "common_*(context).\n");
1332 fprintf(ofp
, "# See the perf-script-python Documentation for the list "
1333 "of available functions.\n\n");
1335 fprintf(ofp
, "import os\n");
1336 fprintf(ofp
, "import sys\n\n");
1338 fprintf(ofp
, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1339 fprintf(ofp
, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1340 fprintf(ofp
, "\nfrom perf_trace_context import *\n");
1341 fprintf(ofp
, "from Core import *\n\n\n");
1343 fprintf(ofp
, "def trace_begin():\n");
1344 fprintf(ofp
, "\tprint \"in trace_begin\"\n\n");
1346 fprintf(ofp
, "def trace_end():\n");
1347 fprintf(ofp
, "\tprint \"in trace_end\"\n\n");
1349 while ((event
= trace_find_next_event(pevent
, event
))) {
1350 fprintf(ofp
, "def %s__%s(", event
->system
, event
->name
);
1351 fprintf(ofp
, "event_name, ");
1352 fprintf(ofp
, "context, ");
1353 fprintf(ofp
, "common_cpu,\n");
1354 fprintf(ofp
, "\tcommon_secs, ");
1355 fprintf(ofp
, "common_nsecs, ");
1356 fprintf(ofp
, "common_pid, ");
1357 fprintf(ofp
, "common_comm,\n\t");
1358 fprintf(ofp
, "common_callchain, ");
1363 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1366 if (++count
% 5 == 0)
1367 fprintf(ofp
, "\n\t");
1369 fprintf(ofp
, "%s", f
->name
);
1373 if (++count
% 5 == 0)
1374 fprintf(ofp
, "\n\t\t");
1375 fprintf(ofp
, "perf_sample_dict");
1377 fprintf(ofp
, "):\n");
1379 fprintf(ofp
, "\t\tprint_header(event_name, common_cpu, "
1380 "common_secs, common_nsecs,\n\t\t\t"
1381 "common_pid, common_comm)\n\n");
1383 fprintf(ofp
, "\t\tprint \"");
1388 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1391 if (count
&& count
% 3 == 0) {
1392 fprintf(ofp
, "\" \\\n\t\t\"");
1396 fprintf(ofp
, "%s=", f
->name
);
1397 if (f
->flags
& FIELD_IS_STRING
||
1398 f
->flags
& FIELD_IS_FLAG
||
1399 f
->flags
& FIELD_IS_ARRAY
||
1400 f
->flags
& FIELD_IS_SYMBOLIC
)
1401 fprintf(ofp
, "%%s");
1402 else if (f
->flags
& FIELD_IS_SIGNED
)
1403 fprintf(ofp
, "%%d");
1405 fprintf(ofp
, "%%u");
1408 fprintf(ofp
, "\" %% \\\n\t\t(");
1413 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1417 if (++count
% 5 == 0)
1418 fprintf(ofp
, "\n\t\t");
1420 if (f
->flags
& FIELD_IS_FLAG
) {
1421 if ((count
- 1) % 5 != 0) {
1422 fprintf(ofp
, "\n\t\t");
1425 fprintf(ofp
, "flag_str(\"");
1426 fprintf(ofp
, "%s__%s\", ", event
->system
,
1428 fprintf(ofp
, "\"%s\", %s)", f
->name
,
1430 } else if (f
->flags
& FIELD_IS_SYMBOLIC
) {
1431 if ((count
- 1) % 5 != 0) {
1432 fprintf(ofp
, "\n\t\t");
1435 fprintf(ofp
, "symbol_str(\"");
1436 fprintf(ofp
, "%s__%s\", ", event
->system
,
1438 fprintf(ofp
, "\"%s\", %s)", f
->name
,
1441 fprintf(ofp
, "%s", f
->name
);
1444 fprintf(ofp
, ")\n\n");
1446 fprintf(ofp
, "\t\tprint 'Sample: {'+"
1447 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
1449 fprintf(ofp
, "\t\tfor node in common_callchain:");
1450 fprintf(ofp
, "\n\t\t\tif 'sym' in node:");
1451 fprintf(ofp
, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1452 fprintf(ofp
, "\n\t\t\telse:");
1453 fprintf(ofp
, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1454 fprintf(ofp
, "\t\tprint \"\\n\"\n\n");
1458 fprintf(ofp
, "def trace_unhandled(event_name, context, "
1459 "event_fields_dict, perf_sample_dict):\n");
1461 fprintf(ofp
, "\t\tprint get_dict_as_string(event_fields_dict)\n");
1462 fprintf(ofp
, "\t\tprint 'Sample: {'+"
1463 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
1465 fprintf(ofp
, "def print_header("
1466 "event_name, cpu, secs, nsecs, pid, comm):\n"
1467 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1468 "(event_name, cpu, secs, nsecs, pid, comm),\n\n");
1470 fprintf(ofp
, "def get_dict_as_string(a_dict, delimiter=' '):\n"
1471 "\treturn delimiter.join"
1472 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
1476 fprintf(stderr
, "generated Python script: %s\n", fname
);
1481 struct scripting_ops python_scripting_ops
= {
1483 .start_script
= python_start_script
,
1484 .flush_script
= python_flush_script
,
1485 .stop_script
= python_stop_script
,
1486 .process_event
= python_process_event
,
1487 .process_stat
= python_process_stat
,
1488 .process_stat_interval
= python_process_stat_interval
,
1489 .generate_script
= python_generate_script
,