Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / perf / util / scripting-engines / trace-event-python.c
blobea070883c5930de28edf8326598a5d8e205ffc63
1 /*
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
22 #include <Python.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <errno.h>
30 #include <linux/bitmap.h>
31 #include <linux/compiler.h>
32 #include <linux/time64.h>
34 #include "../../perf.h"
35 #include "../debug.h"
36 #include "../callchain.h"
37 #include "../evsel.h"
38 #include "../util.h"
39 #include "../event.h"
40 #include "../thread.h"
41 #include "../comm.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"
48 #include "cpumap.h"
49 #include "print_binary.h"
50 #include "stat.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);
59 #define MAX_FIELDS 64
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;
69 struct tables {
70 struct db_export dbe;
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;
82 bool db_export_mode;
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)
90 PyErr_Print();
91 Py_FatalError("problem in Python trace event handler");
92 // Py_FatalError does not return
93 // but we have to make the compiler happy
94 abort();
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);
105 Py_DECREF(val);
108 static PyObject *get_handler(const char *handler_name)
110 PyObject *handler;
112 handler = PyDict_GetItemString(main_dict, handler_name);
113 if (handler && !PyCallable_Check(handler))
114 return NULL;
115 return handler;
118 static int get_argument_count(PyObject *handler)
120 int arg_count = 0;
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,
127 "func_code");
128 if (PyErr_Occurred()) {
129 PyErr_Clear();
130 code_obj = PyObject_GetAttrString(handler,
131 "__code__");
133 PyErr_Clear();
134 if (code_obj) {
135 PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
136 "co_argcount");
137 if (arg_count_obj) {
138 arg_count = (int) PyInt_AsLong(arg_count_obj);
139 Py_DECREF(arg_count_obj);
141 Py_DECREF(code_obj);
143 return arg_count;
146 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
148 PyObject *retval;
150 retval = PyObject_CallObject(handler, args);
151 if (retval == NULL)
152 handler_call_die(die_msg);
153 Py_DECREF(retval);
156 static void try_call_object(const char *handler_name, PyObject *args)
158 PyObject *handler;
160 handler = get_handler(handler_name);
161 if (handler)
162 call_object(handler, args, handler_name);
165 static void define_value(enum print_arg_type field_type,
166 const char *ev_name,
167 const char *field_name,
168 const char *field_value,
169 const char *field_str)
171 const char *handler_name = "define_flag_value";
172 PyObject *t;
173 unsigned long long value;
174 unsigned n = 0;
176 if (field_type == PRINT_SYMBOL)
177 handler_name = "define_symbolic_value";
179 t = PyTuple_New(4);
180 if (!t)
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);
192 Py_DECREF(t);
195 static void define_values(enum print_arg_type field_type,
196 struct print_flag_sym *field,
197 const char *ev_name,
198 const char *field_name)
200 define_value(field_type, ev_name, field_name, field->value,
201 field->str);
203 if (field->next)
204 define_values(field_type, field->next, ev_name, field_name);
207 static void define_field(enum print_arg_type field_type,
208 const char *ev_name,
209 const char *field_name,
210 const char *delim)
212 const char *handler_name = "define_flag_field";
213 PyObject *t;
214 unsigned n = 0;
216 if (field_type == PRINT_SYMBOL)
217 handler_name = "define_symbolic_field";
219 if (field_type == PRINT_FLAGS)
220 t = PyTuple_New(3);
221 else
222 t = PyTuple_New(2);
223 if (!t)
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);
233 Py_DECREF(t);
236 static void define_event_symbols(struct event_format *event,
237 const char *ev_name,
238 struct print_arg *args)
240 if (args == NULL)
241 return;
243 switch (args->type) {
244 case PRINT_NULL:
245 break;
246 case PRINT_ATOM:
247 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
248 args->atom.atom);
249 zero_flag_atom = 0;
250 break;
251 case PRINT_FIELD:
252 free(cur_field_name);
253 cur_field_name = strdup(args->field.name);
254 break;
255 case PRINT_FLAGS:
256 define_event_symbols(event, ev_name, args->flags.field);
257 define_field(PRINT_FLAGS, ev_name, cur_field_name,
258 args->flags.delim);
259 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
260 cur_field_name);
261 break;
262 case PRINT_SYMBOL:
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,
266 cur_field_name);
267 break;
268 case PRINT_HEX:
269 case PRINT_HEX_STR:
270 define_event_symbols(event, ev_name, args->hex.field);
271 define_event_symbols(event, ev_name, args->hex.size);
272 break;
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);
277 break;
278 case PRINT_STRING:
279 break;
280 case PRINT_TYPE:
281 define_event_symbols(event, ev_name, args->typecast.item);
282 break;
283 case PRINT_OP:
284 if (strcmp(args->op.op, ":") == 0)
285 zero_flag_atom = 1;
286 define_event_symbols(event, ev_name, args->op.left);
287 define_event_symbols(event, ev_name, args->op.right);
288 break;
289 default:
290 /* gcc warns for these? */
291 case PRINT_BSTRING:
292 case PRINT_DYNAMIC_ARRAY:
293 case PRINT_DYNAMIC_ARRAY_LEN:
294 case PRINT_FUNC:
295 case PRINT_BITMASK:
296 /* we should warn... */
297 return;
300 if (args->next)
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;
312 if (is_array) {
313 list = PyList_New(field->arraylen);
314 item_size = field->size / field->arraylen;
315 n_items = field->arraylen;
316 } else {
317 item_size = field->size;
318 n_items = 1;
321 for (i = 0; i < n_items; i++) {
323 val = read_size(event, data + field->offset + i * item_size,
324 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);
329 else
330 obj = PyLong_FromLongLong(val);
331 } else {
332 if (val <= LONG_MAX)
333 obj = PyInt_FromLong(val);
334 else
335 obj = PyLong_FromUnsignedLongLong(val);
337 if (is_array)
338 PyList_SET_ITEM(list, i, obj);
340 if (is_array)
341 obj = list;
342 return obj;
346 static PyObject *python_process_callchain(struct perf_sample *sample,
347 struct perf_evsel *evsel,
348 struct addr_location *al)
350 PyObject *pylist;
352 pylist = PyList_New(0);
353 if (!pylist)
354 Py_FatalError("couldn't create Python list");
356 if (!symbol_conf.use_callchain || !sample->callchain)
357 goto exit;
359 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
360 sample, NULL, NULL,
361 scripting_max_stack) != 0) {
362 pr_err("Failed to resolve callchain. Skipping\n");
363 goto exit;
365 callchain_cursor_commit(&callchain_cursor);
368 while (1) {
369 PyObject *pyelem;
370 struct callchain_cursor_node *node;
371 node = callchain_cursor_current(&callchain_cursor);
372 if (!node)
373 break;
375 pyelem = PyDict_New();
376 if (!pyelem)
377 Py_FatalError("couldn't create Python dictionary");
380 pydict_set_item_string_decref(pyelem, "ip",
381 PyLong_FromUnsignedLongLong(node->ip));
383 if (node->sym) {
384 PyObject *pysym = PyDict_New();
385 if (!pysym)
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);
399 if (node->map) {
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;
405 else
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);
414 Py_DECREF(pyelem);
417 exit:
418 return pylist;
421 static PyObject *get_sample_value_as_tuple(struct sample_read_value *value)
423 PyObject *t;
425 t = PyTuple_New(2);
426 if (!t)
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));
430 return t;
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;
438 PyObject *values;
439 unsigned int i;
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);
453 else
454 values = PyList_New(1);
456 if (!values)
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);
464 } else {
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,
474 PyObject *callchain)
476 PyObject *dict, *dict_sample;
478 dict = PyDict_New();
479 if (!dict)
480 Py_FatalError("couldn't create Python dictionary");
482 dict_sample = PyDict_New();
483 if (!dict_sample)
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)));
511 if (al->map) {
512 pydict_set_item_string_decref(dict, "dso",
513 PyString_FromString(al->map->dso->name));
515 if (al->sym) {
516 pydict_set_item_string_decref(dict, "symbol",
517 PyString_FromString(al->sym->name));
520 pydict_set_item_string_decref(dict, "callchain", callchain);
522 return dict;
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;
534 unsigned long s, ns;
535 unsigned n = 0;
536 int pid;
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";
543 if (!event) {
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);
557 if (!handler) {
558 handler = get_handler(default_handler_name);
559 if (!handler)
560 return;
561 dict = PyDict_New();
562 if (!dict)
563 Py_FatalError("couldn't create Python dict");
566 t = PyTuple_New(MAX_FIELDS);
567 if (!t)
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);
582 /* ip unwinding */
583 callchain = python_process_callchain(sample, evsel, al);
584 /* Need an additional reference for the perf_sample dict */
585 Py_INCREF(callchain);
587 if (!dict) {
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);
594 } else {
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;
608 len = field->size;
609 if (field->flags & FIELD_IS_DYNAMIC) {
610 val = pevent_read_number(scripting_context->pevent,
611 data + offset, len);
612 offset = val;
613 len = offset >> 16;
614 offset &= 0xffff;
616 if (field->flags & FIELD_IS_STRING &&
617 is_printable_array(data + offset, len)) {
618 obj = PyString_FromString((char *) data + offset);
619 } else {
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);
626 if (!dict)
627 PyTuple_SetItem(t, n++, obj);
628 else
629 pydict_set_item_string_decref(dict, field->name, obj);
633 if (dict)
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,
638 callchain);
639 PyTuple_SetItem(t, n++, all_entries_dict);
640 } else {
641 Py_DECREF(callchain);
644 if (_PyTuple_Resize(&t, n) == -1)
645 Py_FatalError("error resizing Python tuple");
647 if (!dict) {
648 call_object(handler, t, handler_name);
649 } else {
650 call_object(handler, t, default_handler_name);
651 Py_DECREF(dict);
654 Py_XDECREF(all_entries_dict);
655 Py_DECREF(t);
658 static PyObject *tuple_new(unsigned int sz)
660 PyObject *t;
662 t = PyTuple_New(sz);
663 if (!t)
664 Py_FatalError("couldn't create Python tuple");
665 return t;
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));
672 #endif
673 #if BITS_PER_LONG == 32
674 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
675 #endif
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);
691 PyObject *t;
693 t = tuple_new(2);
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");
700 Py_DECREF(t);
702 return 0;
705 static int python_export_machine(struct db_export *dbe,
706 struct machine *machine)
708 struct tables *tables = container_of(dbe, struct tables, dbe);
709 PyObject *t;
711 t = tuple_new(3);
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");
719 Py_DECREF(t);
721 return 0;
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);
728 PyObject *t;
730 t = tuple_new(5);
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");
740 Py_DECREF(t);
742 return 0;
745 static int python_export_comm(struct db_export *dbe, struct comm *comm)
747 struct tables *tables = container_of(dbe, struct tables, dbe);
748 PyObject *t;
750 t = tuple_new(2);
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");
757 Py_DECREF(t);
759 return 0;
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);
766 PyObject *t;
768 t = tuple_new(3);
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");
776 Py_DECREF(t);
778 return 0;
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];
786 PyObject *t;
788 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
790 t = tuple_new(5);
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");
800 Py_DECREF(t);
802 return 0;
805 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
806 struct dso *dso)
808 struct tables *tables = container_of(dbe, struct tables, dbe);
809 u64 *sym_db_id = symbol__priv(sym);
810 PyObject *t;
812 t = tuple_new(6);
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");
823 Py_DECREF(t);
825 return 0;
828 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
829 const char *name)
831 struct tables *tables = container_of(dbe, struct tables, dbe);
832 PyObject *t;
834 t = tuple_new(2);
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");
841 Py_DECREF(t);
843 return 0;
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);
850 PyObject *t;
852 t = tuple_new(22);
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");
879 Py_DECREF(t);
881 return 0;
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);
887 PyObject *t;
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;
893 t = tuple_new(4);
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");
902 Py_DECREF(t);
904 return 0;
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;
912 PyObject *t;
914 t = tuple_new(11);
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");
930 Py_DECREF(t);
932 return 0;
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];
948 unsigned n = 0;
950 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
952 handler = get_handler(handler_name);
953 if (!handler)
954 return;
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);
961 if (!t)
962 Py_FatalError("couldn't create Python tuple");
964 /* ip unwinding */
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);
974 Py_DECREF(dict);
975 Py_DECREF(t);
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);
988 break;
989 /* Reserve for future process_hw/sw/raw APIs */
990 default:
991 if (tables->db_export_mode)
992 db_export__sample(&tables->dbe, event, sample, evsel, al);
993 else
994 python_process_general_event(sample, evsel, al);
998 static void get_handler_name(char *str, size_t size,
999 struct perf_evsel *evsel)
1001 char *p = str;
1003 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
1005 while ((p = strchr(p, ':'))) {
1006 *p = '_';
1007 p++;
1011 static void
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];
1017 int n = 0;
1019 t = PyTuple_New(MAX_FIELDS);
1020 if (!t)
1021 Py_FatalError("couldn't create Python tuple");
1023 get_handler_name(handler_name, sizeof(handler_name),
1024 counter);
1026 handler = get_handler(handler_name);
1027 if (!handler) {
1028 pr_debug("can't find python handler %s\n", handler_name);
1029 return;
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);
1045 Py_DECREF(t);
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;
1053 int cpu, thread;
1055 if (config->aggr_mode == AGGR_GLOBAL) {
1056 process_stat(counter, -1, -1, tstamp,
1057 &counter->counts->aggr);
1058 return;
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";
1074 int n = 0;
1076 t = PyTuple_New(MAX_FIELDS);
1077 if (!t)
1078 Py_FatalError("couldn't create Python tuple");
1080 handler = get_handler(handler_name);
1081 if (!handler) {
1082 pr_debug("can't find python handler %s\n", handler_name);
1083 return;
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);
1093 Py_DECREF(t);
1096 static int run_start_sub(void)
1098 main_module = PyImport_AddModule("__main__");
1099 if (main_module == NULL)
1100 return -1;
1101 Py_INCREF(main_module);
1103 main_dict = PyModule_GetDict(main_module);
1104 if (main_dict == NULL)
1105 goto error;
1106 Py_INCREF(main_dict);
1108 try_call_object("trace_begin", NULL);
1110 return 0;
1112 error:
1113 Py_XDECREF(main_dict);
1114 Py_XDECREF(main_module);
1115 return -1;
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; \
1122 } while (0)
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;
1135 int ret;
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)
1143 return;
1145 ret = PyObject_IsTrue(db_export_mode);
1146 if (ret == -1)
1147 handler_call_die(perf_db_export_mode);
1148 if (!ret)
1149 return;
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);
1156 if (ret == -1)
1157 handler_call_die(perf_db_export_calls);
1158 export_calls = !!ret;
1161 if (export_calls) {
1162 tables->dbe.crp =
1163 call_return_processor__new(python_process_call_return,
1164 &tables->dbe);
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);
1175 if (ret == -1)
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;
1189 else
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;
1222 char buf[PATH_MAX];
1223 int i, err = 0;
1224 FILE *fp;
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];
1231 Py_Initialize();
1233 initperf_trace_context();
1235 PySys_SetArgv(argc + 1, (char **)command_line);
1237 fp = fopen(script, "r");
1238 if (!fp) {
1239 sprintf(buf, "Can't open python script \"%s\"", script);
1240 perror(buf);
1241 err = -1;
1242 goto error;
1245 err = PyRun_SimpleFile(fp, script);
1246 if (err) {
1247 fprintf(stderr, "Error running python script %s\n", script);
1248 goto error;
1251 err = run_start_sub();
1252 if (err) {
1253 fprintf(stderr, "Error starting python script %s\n", script);
1254 goto error;
1257 set_table_handlers(tables);
1259 if (tables->db_export_mode) {
1260 err = db_export__branch_types(&tables->dbe);
1261 if (err)
1262 goto error;
1265 free(command_line);
1267 return err;
1268 error:
1269 Py_Finalize();
1270 free(command_line);
1272 return err;
1275 static int python_flush_script(void)
1277 struct tables *tables = &tables_global;
1279 return db_export__flush(&tables->dbe);
1283 * Stop trace script
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);
1295 Py_Finalize();
1297 return 0;
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;
1306 FILE *ofp;
1308 sprintf(fname, "%s.py", outfile);
1309 ofp = fopen(fname, "w");
1310 if (ofp == NULL) {
1311 fprintf(stderr, "couldn't open %s\n", fname);
1312 return -1;
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, ");
1360 not_first = 0;
1361 count = 0;
1363 for (f = event->format.fields; f; f = f->next) {
1364 if (not_first++)
1365 fprintf(ofp, ", ");
1366 if (++count % 5 == 0)
1367 fprintf(ofp, "\n\t");
1369 fprintf(ofp, "%s", f->name);
1371 if (not_first++)
1372 fprintf(ofp, ", ");
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 \"");
1385 not_first = 0;
1386 count = 0;
1388 for (f = event->format.fields; f; f = f->next) {
1389 if (not_first++)
1390 fprintf(ofp, ", ");
1391 if (count && count % 3 == 0) {
1392 fprintf(ofp, "\" \\\n\t\t\"");
1394 count++;
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");
1404 else
1405 fprintf(ofp, "%%u");
1408 fprintf(ofp, "\" %% \\\n\t\t(");
1410 not_first = 0;
1411 count = 0;
1413 for (f = event->format.fields; f; f = f->next) {
1414 if (not_first++)
1415 fprintf(ofp, ", ");
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");
1423 count = 4;
1425 fprintf(ofp, "flag_str(\"");
1426 fprintf(ofp, "%s__%s\", ", event->system,
1427 event->name);
1428 fprintf(ofp, "\"%s\", %s)", f->name,
1429 f->name);
1430 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1431 if ((count - 1) % 5 != 0) {
1432 fprintf(ofp, "\n\t\t");
1433 count = 4;
1435 fprintf(ofp, "symbol_str(\"");
1436 fprintf(ofp, "%s__%s\", ", event->system,
1437 event->name);
1438 fprintf(ofp, "\"%s\", %s)", f->name,
1439 f->name);
1440 } else
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");
1474 fclose(ofp);
1476 fprintf(stderr, "generated Python script: %s\n", fname);
1478 return 0;
1481 struct scripting_ops python_scripting_ops = {
1482 .name = "Python",
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,