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>
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 "../machine.h"
47 #include "../call-path.h"
48 #include "thread_map.h"
50 #include "print_binary.h"
53 PyMODINIT_FUNC initperf_trace_context(void);
55 #define TRACE_EVENT_TYPE_MAX \
56 ((1 << (sizeof(unsigned short) * 8)) - 1)
58 static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
61 #define N_COMMON_FIELDS 7
63 extern struct scripting_context *scripting_context;
65 static char *cur_field_name;
66 static int zero_flag_atom;
68 static PyObject *main_module, *main_dict;
72 PyObject *evsel_handler;
73 PyObject *machine_handler;
74 PyObject *thread_handler;
75 PyObject *comm_handler;
76 PyObject *comm_thread_handler;
77 PyObject *dso_handler;
78 PyObject *symbol_handler;
79 PyObject *branch_type_handler;
80 PyObject *sample_handler;
81 PyObject *call_path_handler;
82 PyObject *call_return_handler;
86 static struct tables tables_global;
88 static void handler_call_die(const char *handler_name) __noreturn;
89 static void handler_call_die(const char *handler_name)
92 Py_FatalError("problem in Python trace event handler");
93 // Py_FatalError does not return
94 // but we have to make the compiler happy
99 * Insert val into into the dictionary and decrement the reference counter.
100 * This is necessary for dictionaries since PyDict_SetItemString() does not
101 * steal a reference, as opposed to PyTuple_SetItem().
103 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
105 PyDict_SetItemString(dict, key, val);
109 static PyObject *get_handler(const char *handler_name)
113 handler = PyDict_GetItemString(main_dict, handler_name);
114 if (handler && !PyCallable_Check(handler))
119 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
123 retval = PyObject_CallObject(handler, args);
125 handler_call_die(die_msg);
129 static void try_call_object(const char *handler_name, PyObject *args)
133 handler = get_handler(handler_name);
135 call_object(handler, args, handler_name);
138 static void define_value(enum print_arg_type field_type,
140 const char *field_name,
141 const char *field_value,
142 const char *field_str)
144 const char *handler_name = "define_flag_value";
146 unsigned long long value;
149 if (field_type == PRINT_SYMBOL)
150 handler_name = "define_symbolic_value";
154 Py_FatalError("couldn't create Python tuple");
156 value = eval_flag(field_value);
158 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
159 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
160 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
161 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
163 try_call_object(handler_name, t);
168 static void define_values(enum print_arg_type field_type,
169 struct print_flag_sym *field,
171 const char *field_name)
173 define_value(field_type, ev_name, field_name, field->value,
177 define_values(field_type, field->next, ev_name, field_name);
180 static void define_field(enum print_arg_type field_type,
182 const char *field_name,
185 const char *handler_name = "define_flag_field";
189 if (field_type == PRINT_SYMBOL)
190 handler_name = "define_symbolic_field";
192 if (field_type == PRINT_FLAGS)
197 Py_FatalError("couldn't create Python tuple");
199 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
200 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
201 if (field_type == PRINT_FLAGS)
202 PyTuple_SetItem(t, n++, PyString_FromString(delim));
204 try_call_object(handler_name, t);
209 static void define_event_symbols(struct event_format *event,
211 struct print_arg *args)
216 switch (args->type) {
220 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
225 free(cur_field_name);
226 cur_field_name = strdup(args->field.name);
229 define_event_symbols(event, ev_name, args->flags.field);
230 define_field(PRINT_FLAGS, ev_name, cur_field_name,
232 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
236 define_event_symbols(event, ev_name, args->symbol.field);
237 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
238 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
243 define_event_symbols(event, ev_name, args->hex.field);
244 define_event_symbols(event, ev_name, args->hex.size);
246 case PRINT_INT_ARRAY:
247 define_event_symbols(event, ev_name, args->int_array.field);
248 define_event_symbols(event, ev_name, args->int_array.count);
249 define_event_symbols(event, ev_name, args->int_array.el_size);
254 define_event_symbols(event, ev_name, args->typecast.item);
257 if (strcmp(args->op.op, ":") == 0)
259 define_event_symbols(event, ev_name, args->op.left);
260 define_event_symbols(event, ev_name, args->op.right);
263 /* gcc warns for these? */
265 case PRINT_DYNAMIC_ARRAY:
266 case PRINT_DYNAMIC_ARRAY_LEN:
269 /* we should warn... */
274 define_event_symbols(event, ev_name, args->next);
277 static PyObject *get_field_numeric_entry(struct event_format *event,
278 struct format_field *field, void *data)
280 bool is_array = field->flags & FIELD_IS_ARRAY;
281 PyObject *obj = NULL, *list = NULL;
282 unsigned long long val;
283 unsigned int item_size, n_items, i;
286 list = PyList_New(field->arraylen);
287 item_size = field->size / field->arraylen;
288 n_items = field->arraylen;
290 item_size = field->size;
294 for (i = 0; i < n_items; i++) {
296 val = read_size(event, data + field->offset + i * item_size,
298 if (field->flags & FIELD_IS_SIGNED) {
299 if ((long long)val >= LONG_MIN &&
300 (long long)val <= LONG_MAX)
301 obj = PyInt_FromLong(val);
303 obj = PyLong_FromLongLong(val);
306 obj = PyInt_FromLong(val);
308 obj = PyLong_FromUnsignedLongLong(val);
311 PyList_SET_ITEM(list, i, obj);
319 static PyObject *python_process_callchain(struct perf_sample *sample,
320 struct perf_evsel *evsel,
321 struct addr_location *al)
325 pylist = PyList_New(0);
327 Py_FatalError("couldn't create Python list");
329 if (!symbol_conf.use_callchain || !sample->callchain)
332 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
334 scripting_max_stack) != 0) {
335 pr_err("Failed to resolve callchain. Skipping\n");
338 callchain_cursor_commit(&callchain_cursor);
343 struct callchain_cursor_node *node;
344 node = callchain_cursor_current(&callchain_cursor);
348 pyelem = PyDict_New();
350 Py_FatalError("couldn't create Python dictionary");
353 pydict_set_item_string_decref(pyelem, "ip",
354 PyLong_FromUnsignedLongLong(node->ip));
357 PyObject *pysym = PyDict_New();
359 Py_FatalError("couldn't create Python dictionary");
360 pydict_set_item_string_decref(pysym, "start",
361 PyLong_FromUnsignedLongLong(node->sym->start));
362 pydict_set_item_string_decref(pysym, "end",
363 PyLong_FromUnsignedLongLong(node->sym->end));
364 pydict_set_item_string_decref(pysym, "binding",
365 PyInt_FromLong(node->sym->binding));
366 pydict_set_item_string_decref(pysym, "name",
367 PyString_FromStringAndSize(node->sym->name,
368 node->sym->namelen));
369 pydict_set_item_string_decref(pyelem, "sym", pysym);
373 struct map *map = node->map;
374 const char *dsoname = "[unknown]";
375 if (map && map->dso) {
376 if (symbol_conf.show_kernel_path && map->dso->long_name)
377 dsoname = map->dso->long_name;
379 dsoname = map->dso->name;
381 pydict_set_item_string_decref(pyelem, "dso",
382 PyString_FromString(dsoname));
385 callchain_cursor_advance(&callchain_cursor);
386 PyList_Append(pylist, pyelem);
394 static void python_process_tracepoint(struct perf_sample *sample,
395 struct perf_evsel *evsel,
396 struct addr_location *al)
398 struct event_format *event = evsel->tp_format;
399 PyObject *handler, *context, *t, *obj = NULL, *callchain;
400 PyObject *dict = NULL;
401 static char handler_name[256];
402 struct format_field *field;
406 int cpu = sample->cpu;
407 void *data = sample->raw_data;
408 unsigned long long nsecs = sample->time;
409 const char *comm = thread__comm_str(al->thread);
411 t = PyTuple_New(MAX_FIELDS);
413 Py_FatalError("couldn't create Python tuple");
416 snprintf(handler_name, sizeof(handler_name),
417 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
418 Py_FatalError(handler_name);
421 pid = raw_field_value(event, "common_pid", data);
423 sprintf(handler_name, "%s__%s", event->system, event->name);
425 if (!test_and_set_bit(event->id, events_defined))
426 define_event_symbols(event, handler_name, event->print_fmt.args);
428 handler = get_handler(handler_name);
432 Py_FatalError("couldn't create Python dict");
434 s = nsecs / NSEC_PER_SEC;
435 ns = nsecs - s * NSEC_PER_SEC;
437 scripting_context->event_data = data;
438 scripting_context->pevent = evsel->tp_format->pevent;
440 context = PyCObject_FromVoidPtr(scripting_context, NULL);
442 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
443 PyTuple_SetItem(t, n++, context);
446 callchain = python_process_callchain(sample, evsel, al);
449 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
450 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
451 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
452 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
453 PyTuple_SetItem(t, n++, PyString_FromString(comm));
454 PyTuple_SetItem(t, n++, callchain);
456 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
457 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
458 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
459 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
460 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
461 pydict_set_item_string_decref(dict, "common_callchain", callchain);
463 for (field = event->format.fields; field; field = field->next) {
464 unsigned int offset, len;
465 unsigned long long val;
467 if (field->flags & FIELD_IS_ARRAY) {
468 offset = field->offset;
470 if (field->flags & FIELD_IS_DYNAMIC) {
471 val = pevent_read_number(scripting_context->pevent,
477 if (field->flags & FIELD_IS_STRING &&
478 is_printable_array(data + offset, len)) {
479 obj = PyString_FromString((char *) data + offset);
481 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
482 field->flags &= ~FIELD_IS_STRING;
484 } else { /* FIELD_IS_NUMERIC */
485 obj = get_field_numeric_entry(event, field, data);
488 PyTuple_SetItem(t, n++, obj);
490 pydict_set_item_string_decref(dict, field->name, obj);
495 PyTuple_SetItem(t, n++, dict);
497 if (_PyTuple_Resize(&t, n) == -1)
498 Py_FatalError("error resizing Python tuple");
501 call_object(handler, t, handler_name);
503 try_call_object("trace_unhandled", t);
510 static PyObject *tuple_new(unsigned int sz)
516 Py_FatalError("couldn't create Python tuple");
520 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
522 #if BITS_PER_LONG == 64
523 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
525 #if BITS_PER_LONG == 32
526 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
530 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
532 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
535 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
537 return PyTuple_SetItem(t, pos, PyString_FromString(s));
540 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
542 struct tables *tables = container_of(dbe, struct tables, dbe);
547 tuple_set_u64(t, 0, evsel->db_id);
548 tuple_set_string(t, 1, perf_evsel__name(evsel));
550 call_object(tables->evsel_handler, t, "evsel_table");
557 static int python_export_machine(struct db_export *dbe,
558 struct machine *machine)
560 struct tables *tables = container_of(dbe, struct tables, dbe);
565 tuple_set_u64(t, 0, machine->db_id);
566 tuple_set_s32(t, 1, machine->pid);
567 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
569 call_object(tables->machine_handler, t, "machine_table");
576 static int python_export_thread(struct db_export *dbe, struct thread *thread,
577 u64 main_thread_db_id, struct machine *machine)
579 struct tables *tables = container_of(dbe, struct tables, dbe);
584 tuple_set_u64(t, 0, thread->db_id);
585 tuple_set_u64(t, 1, machine->db_id);
586 tuple_set_u64(t, 2, main_thread_db_id);
587 tuple_set_s32(t, 3, thread->pid_);
588 tuple_set_s32(t, 4, thread->tid);
590 call_object(tables->thread_handler, t, "thread_table");
597 static int python_export_comm(struct db_export *dbe, struct comm *comm)
599 struct tables *tables = container_of(dbe, struct tables, dbe);
604 tuple_set_u64(t, 0, comm->db_id);
605 tuple_set_string(t, 1, comm__str(comm));
607 call_object(tables->comm_handler, t, "comm_table");
614 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
615 struct comm *comm, struct thread *thread)
617 struct tables *tables = container_of(dbe, struct tables, dbe);
622 tuple_set_u64(t, 0, db_id);
623 tuple_set_u64(t, 1, comm->db_id);
624 tuple_set_u64(t, 2, thread->db_id);
626 call_object(tables->comm_thread_handler, t, "comm_thread_table");
633 static int python_export_dso(struct db_export *dbe, struct dso *dso,
634 struct machine *machine)
636 struct tables *tables = container_of(dbe, struct tables, dbe);
637 char sbuild_id[SBUILD_ID_SIZE];
640 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
644 tuple_set_u64(t, 0, dso->db_id);
645 tuple_set_u64(t, 1, machine->db_id);
646 tuple_set_string(t, 2, dso->short_name);
647 tuple_set_string(t, 3, dso->long_name);
648 tuple_set_string(t, 4, sbuild_id);
650 call_object(tables->dso_handler, t, "dso_table");
657 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
660 struct tables *tables = container_of(dbe, struct tables, dbe);
661 u64 *sym_db_id = symbol__priv(sym);
666 tuple_set_u64(t, 0, *sym_db_id);
667 tuple_set_u64(t, 1, dso->db_id);
668 tuple_set_u64(t, 2, sym->start);
669 tuple_set_u64(t, 3, sym->end);
670 tuple_set_s32(t, 4, sym->binding);
671 tuple_set_string(t, 5, sym->name);
673 call_object(tables->symbol_handler, t, "symbol_table");
680 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
683 struct tables *tables = container_of(dbe, struct tables, dbe);
688 tuple_set_s32(t, 0, branch_type);
689 tuple_set_string(t, 1, name);
691 call_object(tables->branch_type_handler, t, "branch_type_table");
698 static int python_export_sample(struct db_export *dbe,
699 struct export_sample *es)
701 struct tables *tables = container_of(dbe, struct tables, dbe);
706 tuple_set_u64(t, 0, es->db_id);
707 tuple_set_u64(t, 1, es->evsel->db_id);
708 tuple_set_u64(t, 2, es->al->machine->db_id);
709 tuple_set_u64(t, 3, es->al->thread->db_id);
710 tuple_set_u64(t, 4, es->comm_db_id);
711 tuple_set_u64(t, 5, es->dso_db_id);
712 tuple_set_u64(t, 6, es->sym_db_id);
713 tuple_set_u64(t, 7, es->offset);
714 tuple_set_u64(t, 8, es->sample->ip);
715 tuple_set_u64(t, 9, es->sample->time);
716 tuple_set_s32(t, 10, es->sample->cpu);
717 tuple_set_u64(t, 11, es->addr_dso_db_id);
718 tuple_set_u64(t, 12, es->addr_sym_db_id);
719 tuple_set_u64(t, 13, es->addr_offset);
720 tuple_set_u64(t, 14, es->sample->addr);
721 tuple_set_u64(t, 15, es->sample->period);
722 tuple_set_u64(t, 16, es->sample->weight);
723 tuple_set_u64(t, 17, es->sample->transaction);
724 tuple_set_u64(t, 18, es->sample->data_src);
725 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
726 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
727 tuple_set_u64(t, 21, es->call_path_id);
729 call_object(tables->sample_handler, t, "sample_table");
736 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
738 struct tables *tables = container_of(dbe, struct tables, dbe);
740 u64 parent_db_id, sym_db_id;
742 parent_db_id = cp->parent ? cp->parent->db_id : 0;
743 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
747 tuple_set_u64(t, 0, cp->db_id);
748 tuple_set_u64(t, 1, parent_db_id);
749 tuple_set_u64(t, 2, sym_db_id);
750 tuple_set_u64(t, 3, cp->ip);
752 call_object(tables->call_path_handler, t, "call_path_table");
759 static int python_export_call_return(struct db_export *dbe,
760 struct call_return *cr)
762 struct tables *tables = container_of(dbe, struct tables, dbe);
763 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
768 tuple_set_u64(t, 0, cr->db_id);
769 tuple_set_u64(t, 1, cr->thread->db_id);
770 tuple_set_u64(t, 2, comm_db_id);
771 tuple_set_u64(t, 3, cr->cp->db_id);
772 tuple_set_u64(t, 4, cr->call_time);
773 tuple_set_u64(t, 5, cr->return_time);
774 tuple_set_u64(t, 6, cr->branch_count);
775 tuple_set_u64(t, 7, cr->call_ref);
776 tuple_set_u64(t, 8, cr->return_ref);
777 tuple_set_u64(t, 9, cr->cp->parent->db_id);
778 tuple_set_s32(t, 10, cr->flags);
780 call_object(tables->call_return_handler, t, "call_return_table");
787 static int python_process_call_return(struct call_return *cr, void *data)
789 struct db_export *dbe = data;
791 return db_export__call_return(dbe, cr);
794 static void python_process_general_event(struct perf_sample *sample,
795 struct perf_evsel *evsel,
796 struct addr_location *al)
798 PyObject *handler, *t, *dict, *callchain, *dict_sample;
799 static char handler_name[64];
803 * Use the MAX_FIELDS to make the function expandable, though
804 * currently there is only one item for the tuple.
806 t = PyTuple_New(MAX_FIELDS);
808 Py_FatalError("couldn't create Python tuple");
812 Py_FatalError("couldn't create Python dictionary");
814 dict_sample = PyDict_New();
816 Py_FatalError("couldn't create Python dictionary");
818 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
820 handler = get_handler(handler_name);
824 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
825 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
826 (const char *)&evsel->attr, sizeof(evsel->attr)));
828 pydict_set_item_string_decref(dict_sample, "pid",
829 PyInt_FromLong(sample->pid));
830 pydict_set_item_string_decref(dict_sample, "tid",
831 PyInt_FromLong(sample->tid));
832 pydict_set_item_string_decref(dict_sample, "cpu",
833 PyInt_FromLong(sample->cpu));
834 pydict_set_item_string_decref(dict_sample, "ip",
835 PyLong_FromUnsignedLongLong(sample->ip));
836 pydict_set_item_string_decref(dict_sample, "time",
837 PyLong_FromUnsignedLongLong(sample->time));
838 pydict_set_item_string_decref(dict_sample, "period",
839 PyLong_FromUnsignedLongLong(sample->period));
840 pydict_set_item_string_decref(dict, "sample", dict_sample);
842 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
843 (const char *)sample->raw_data, sample->raw_size));
844 pydict_set_item_string_decref(dict, "comm",
845 PyString_FromString(thread__comm_str(al->thread)));
847 pydict_set_item_string_decref(dict, "dso",
848 PyString_FromString(al->map->dso->name));
851 pydict_set_item_string_decref(dict, "symbol",
852 PyString_FromString(al->sym->name));
856 callchain = python_process_callchain(sample, evsel, al);
857 pydict_set_item_string_decref(dict, "callchain", callchain);
859 PyTuple_SetItem(t, n++, dict);
860 if (_PyTuple_Resize(&t, n) == -1)
861 Py_FatalError("error resizing Python tuple");
863 call_object(handler, t, handler_name);
869 static void python_process_event(union perf_event *event,
870 struct perf_sample *sample,
871 struct perf_evsel *evsel,
872 struct addr_location *al)
874 struct tables *tables = &tables_global;
876 switch (evsel->attr.type) {
877 case PERF_TYPE_TRACEPOINT:
878 python_process_tracepoint(sample, evsel, al);
880 /* Reserve for future process_hw/sw/raw APIs */
882 if (tables->db_export_mode)
883 db_export__sample(&tables->dbe, event, sample, evsel, al);
885 python_process_general_event(sample, evsel, al);
889 static void get_handler_name(char *str, size_t size,
890 struct perf_evsel *evsel)
894 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
896 while ((p = strchr(p, ':'))) {
903 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
904 struct perf_counts_values *count)
906 PyObject *handler, *t;
907 static char handler_name[256];
910 t = PyTuple_New(MAX_FIELDS);
912 Py_FatalError("couldn't create Python tuple");
914 get_handler_name(handler_name, sizeof(handler_name),
917 handler = get_handler(handler_name);
919 pr_debug("can't find python handler %s\n", handler_name);
923 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
924 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
926 tuple_set_u64(t, n++, tstamp);
927 tuple_set_u64(t, n++, count->val);
928 tuple_set_u64(t, n++, count->ena);
929 tuple_set_u64(t, n++, count->run);
931 if (_PyTuple_Resize(&t, n) == -1)
932 Py_FatalError("error resizing Python tuple");
934 call_object(handler, t, handler_name);
939 static void python_process_stat(struct perf_stat_config *config,
940 struct perf_evsel *counter, u64 tstamp)
942 struct thread_map *threads = counter->threads;
943 struct cpu_map *cpus = counter->cpus;
946 if (config->aggr_mode == AGGR_GLOBAL) {
947 process_stat(counter, -1, -1, tstamp,
948 &counter->counts->aggr);
952 for (thread = 0; thread < threads->nr; thread++) {
953 for (cpu = 0; cpu < cpus->nr; cpu++) {
954 process_stat(counter, cpus->map[cpu],
955 thread_map__pid(threads, thread), tstamp,
956 perf_counts(counter->counts, cpu, thread));
961 static void python_process_stat_interval(u64 tstamp)
963 PyObject *handler, *t;
964 static const char handler_name[] = "stat__interval";
967 t = PyTuple_New(MAX_FIELDS);
969 Py_FatalError("couldn't create Python tuple");
971 handler = get_handler(handler_name);
973 pr_debug("can't find python handler %s\n", handler_name);
977 tuple_set_u64(t, n++, tstamp);
979 if (_PyTuple_Resize(&t, n) == -1)
980 Py_FatalError("error resizing Python tuple");
982 call_object(handler, t, handler_name);
987 static int run_start_sub(void)
989 main_module = PyImport_AddModule("__main__");
990 if (main_module == NULL)
992 Py_INCREF(main_module);
994 main_dict = PyModule_GetDict(main_module);
995 if (main_dict == NULL)
997 Py_INCREF(main_dict);
999 try_call_object("trace_begin", NULL);
1004 Py_XDECREF(main_dict);
1005 Py_XDECREF(main_module);
1009 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1010 tables->handler_name = get_handler(#table_name); \
1011 if (tables->handler_name) \
1012 tables->dbe.export_ ## name = python_export_ ## name; \
1015 #define SET_TABLE_HANDLER(name) \
1016 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1018 static void set_table_handlers(struct tables *tables)
1020 const char *perf_db_export_mode = "perf_db_export_mode";
1021 const char *perf_db_export_calls = "perf_db_export_calls";
1022 const char *perf_db_export_callchains = "perf_db_export_callchains";
1023 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1024 bool export_calls = false;
1025 bool export_callchains = false;
1028 memset(tables, 0, sizeof(struct tables));
1029 if (db_export__init(&tables->dbe))
1030 Py_FatalError("failed to initialize export");
1032 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1033 if (!db_export_mode)
1036 ret = PyObject_IsTrue(db_export_mode);
1038 handler_call_die(perf_db_export_mode);
1042 /* handle export calls */
1043 tables->dbe.crp = NULL;
1044 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1045 if (db_export_calls) {
1046 ret = PyObject_IsTrue(db_export_calls);
1048 handler_call_die(perf_db_export_calls);
1049 export_calls = !!ret;
1054 call_return_processor__new(python_process_call_return,
1056 if (!tables->dbe.crp)
1057 Py_FatalError("failed to create calls processor");
1060 /* handle export callchains */
1061 tables->dbe.cpr = NULL;
1062 db_export_callchains = PyDict_GetItemString(main_dict,
1063 perf_db_export_callchains);
1064 if (db_export_callchains) {
1065 ret = PyObject_IsTrue(db_export_callchains);
1067 handler_call_die(perf_db_export_callchains);
1068 export_callchains = !!ret;
1071 if (export_callchains) {
1073 * Attempt to use the call path root from the call return
1074 * processor, if the call return processor is in use. Otherwise,
1075 * we allocate a new call path root. This prevents exporting
1076 * duplicate call path ids when both are in use simultaniously.
1078 if (tables->dbe.crp)
1079 tables->dbe.cpr = tables->dbe.crp->cpr;
1081 tables->dbe.cpr = call_path_root__new();
1083 if (!tables->dbe.cpr)
1084 Py_FatalError("failed to create call path root");
1087 tables->db_export_mode = true;
1089 * Reserve per symbol space for symbol->db_id via symbol__priv()
1091 symbol_conf.priv_size = sizeof(u64);
1093 SET_TABLE_HANDLER(evsel);
1094 SET_TABLE_HANDLER(machine);
1095 SET_TABLE_HANDLER(thread);
1096 SET_TABLE_HANDLER(comm);
1097 SET_TABLE_HANDLER(comm_thread);
1098 SET_TABLE_HANDLER(dso);
1099 SET_TABLE_HANDLER(symbol);
1100 SET_TABLE_HANDLER(branch_type);
1101 SET_TABLE_HANDLER(sample);
1102 SET_TABLE_HANDLER(call_path);
1103 SET_TABLE_HANDLER(call_return);
1107 * Start trace script
1109 static int python_start_script(const char *script, int argc, const char **argv)
1111 struct tables *tables = &tables_global;
1112 const char **command_line;
1117 command_line = malloc((argc + 1) * sizeof(const char *));
1118 command_line[0] = script;
1119 for (i = 1; i < argc + 1; i++)
1120 command_line[i] = argv[i - 1];
1124 initperf_trace_context();
1126 PySys_SetArgv(argc + 1, (char **)command_line);
1128 fp = fopen(script, "r");
1130 sprintf(buf, "Can't open python script \"%s\"", script);
1136 err = PyRun_SimpleFile(fp, script);
1138 fprintf(stderr, "Error running python script %s\n", script);
1142 err = run_start_sub();
1144 fprintf(stderr, "Error starting python script %s\n", script);
1148 set_table_handlers(tables);
1150 if (tables->db_export_mode) {
1151 err = db_export__branch_types(&tables->dbe);
1166 static int python_flush_script(void)
1168 struct tables *tables = &tables_global;
1170 return db_export__flush(&tables->dbe);
1176 static int python_stop_script(void)
1178 struct tables *tables = &tables_global;
1180 try_call_object("trace_end", NULL);
1182 db_export__exit(&tables->dbe);
1184 Py_XDECREF(main_dict);
1185 Py_XDECREF(main_module);
1191 static int python_generate_script(struct pevent *pevent, const char *outfile)
1193 struct event_format *event = NULL;
1194 struct format_field *f;
1195 char fname[PATH_MAX];
1196 int not_first, count;
1199 sprintf(fname, "%s.py", outfile);
1200 ofp = fopen(fname, "w");
1202 fprintf(stderr, "couldn't open %s\n", fname);
1205 fprintf(ofp, "# perf script event handlers, "
1206 "generated by perf script -g python\n");
1208 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1209 " License version 2\n\n");
1211 fprintf(ofp, "# The common_* event handler fields are the most useful "
1212 "fields common to\n");
1214 fprintf(ofp, "# all events. They don't necessarily correspond to "
1215 "the 'common_*' fields\n");
1217 fprintf(ofp, "# in the format files. Those fields not available as "
1218 "handler params can\n");
1220 fprintf(ofp, "# be retrieved using Python functions of the form "
1221 "common_*(context).\n");
1223 fprintf(ofp, "# See the perf-script-python Documentation for the list "
1224 "of available functions.\n\n");
1226 fprintf(ofp, "import os\n");
1227 fprintf(ofp, "import sys\n\n");
1229 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1230 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1231 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1232 fprintf(ofp, "from Core import *\n\n\n");
1234 fprintf(ofp, "def trace_begin():\n");
1235 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1237 fprintf(ofp, "def trace_end():\n");
1238 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1240 while ((event = trace_find_next_event(pevent, event))) {
1241 fprintf(ofp, "def %s__%s(", event->system, event->name);
1242 fprintf(ofp, "event_name, ");
1243 fprintf(ofp, "context, ");
1244 fprintf(ofp, "common_cpu,\n");
1245 fprintf(ofp, "\tcommon_secs, ");
1246 fprintf(ofp, "common_nsecs, ");
1247 fprintf(ofp, "common_pid, ");
1248 fprintf(ofp, "common_comm,\n\t");
1249 fprintf(ofp, "common_callchain, ");
1254 for (f = event->format.fields; f; f = f->next) {
1257 if (++count % 5 == 0)
1258 fprintf(ofp, "\n\t");
1260 fprintf(ofp, "%s", f->name);
1262 fprintf(ofp, "):\n");
1264 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1265 "common_secs, common_nsecs,\n\t\t\t"
1266 "common_pid, common_comm)\n\n");
1268 fprintf(ofp, "\t\tprint \"");
1273 for (f = event->format.fields; f; f = f->next) {
1276 if (count && count % 3 == 0) {
1277 fprintf(ofp, "\" \\\n\t\t\"");
1281 fprintf(ofp, "%s=", f->name);
1282 if (f->flags & FIELD_IS_STRING ||
1283 f->flags & FIELD_IS_FLAG ||
1284 f->flags & FIELD_IS_ARRAY ||
1285 f->flags & FIELD_IS_SYMBOLIC)
1286 fprintf(ofp, "%%s");
1287 else if (f->flags & FIELD_IS_SIGNED)
1288 fprintf(ofp, "%%d");
1290 fprintf(ofp, "%%u");
1293 fprintf(ofp, "\" %% \\\n\t\t(");
1298 for (f = event->format.fields; f; f = f->next) {
1302 if (++count % 5 == 0)
1303 fprintf(ofp, "\n\t\t");
1305 if (f->flags & FIELD_IS_FLAG) {
1306 if ((count - 1) % 5 != 0) {
1307 fprintf(ofp, "\n\t\t");
1310 fprintf(ofp, "flag_str(\"");
1311 fprintf(ofp, "%s__%s\", ", event->system,
1313 fprintf(ofp, "\"%s\", %s)", f->name,
1315 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1316 if ((count - 1) % 5 != 0) {
1317 fprintf(ofp, "\n\t\t");
1320 fprintf(ofp, "symbol_str(\"");
1321 fprintf(ofp, "%s__%s\", ", event->system,
1323 fprintf(ofp, "\"%s\", %s)", f->name,
1326 fprintf(ofp, "%s", f->name);
1329 fprintf(ofp, ")\n\n");
1331 fprintf(ofp, "\t\tfor node in common_callchain:");
1332 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1333 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1334 fprintf(ofp, "\n\t\t\telse:");
1335 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1336 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1340 fprintf(ofp, "def trace_unhandled(event_name, context, "
1341 "event_fields_dict):\n");
1343 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1344 "for k,v in sorted(event_fields_dict.items())])\n\n");
1346 fprintf(ofp, "def print_header("
1347 "event_name, cpu, secs, nsecs, pid, comm):\n"
1348 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1349 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1353 fprintf(stderr, "generated Python script: %s\n", fname);
1358 struct scripting_ops python_scripting_ops = {
1360 .start_script = python_start_script,
1361 .flush_script = python_flush_script,
1362 .stop_script = python_stop_script,
1363 .process_event = python_process_event,
1364 .process_stat = python_process_stat,
1365 .process_stat_interval = python_process_stat_interval,
1366 .generate_script = python_generate_script,