Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * trace-event-python. Feed trace events to an embedded Python interpreter.
3 *
4 * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#include <Python.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdbool.h>
28#include <errno.h>
29#include <linux/bitmap.h>
30#include <linux/time64.h>
31
32#include "../../perf.h"
33#include "../debug.h"
34#include "../callchain.h"
35#include "../evsel.h"
36#include "../util.h"
37#include "../event.h"
38#include "../thread.h"
39#include "../comm.h"
40#include "../machine.h"
41#include "../db-export.h"
42#include "../thread-stack.h"
43#include "../trace-event.h"
44#include "../machine.h"
45#include "../call-path.h"
46#include "thread_map.h"
47#include "cpumap.h"
48#include "stat.h"
49
50PyMODINIT_FUNC initperf_trace_context(void);
51
52#define TRACE_EVENT_TYPE_MAX \
53 ((1 << (sizeof(unsigned short) * 8)) - 1)
54
55static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
56
57#define MAX_FIELDS 64
58#define N_COMMON_FIELDS 7
59
60extern struct scripting_context *scripting_context;
61
62static char *cur_field_name;
63static int zero_flag_atom;
64
65static PyObject *main_module, *main_dict;
66
67struct tables {
68 struct db_export dbe;
69 PyObject *evsel_handler;
70 PyObject *machine_handler;
71 PyObject *thread_handler;
72 PyObject *comm_handler;
73 PyObject *comm_thread_handler;
74 PyObject *dso_handler;
75 PyObject *symbol_handler;
76 PyObject *branch_type_handler;
77 PyObject *sample_handler;
78 PyObject *call_path_handler;
79 PyObject *call_return_handler;
80 bool db_export_mode;
81};
82
83static struct tables tables_global;
84
85static void handler_call_die(const char *handler_name) NORETURN;
86static void handler_call_die(const char *handler_name)
87{
88 PyErr_Print();
89 Py_FatalError("problem in Python trace event handler");
90 // Py_FatalError does not return
91 // but we have to make the compiler happy
92 abort();
93}
94
95/*
96 * Insert val into into the dictionary and decrement the reference counter.
97 * This is necessary for dictionaries since PyDict_SetItemString() does not
98 * steal a reference, as opposed to PyTuple_SetItem().
99 */
100static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
101{
102 PyDict_SetItemString(dict, key, val);
103 Py_DECREF(val);
104}
105
106static PyObject *get_handler(const char *handler_name)
107{
108 PyObject *handler;
109
110 handler = PyDict_GetItemString(main_dict, handler_name);
111 if (handler && !PyCallable_Check(handler))
112 return NULL;
113 return handler;
114}
115
116static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
117{
118 PyObject *retval;
119
120 retval = PyObject_CallObject(handler, args);
121 if (retval == NULL)
122 handler_call_die(die_msg);
123 Py_DECREF(retval);
124}
125
126static void try_call_object(const char *handler_name, PyObject *args)
127{
128 PyObject *handler;
129
130 handler = get_handler(handler_name);
131 if (handler)
132 call_object(handler, args, handler_name);
133}
134
135static void define_value(enum print_arg_type field_type,
136 const char *ev_name,
137 const char *field_name,
138 const char *field_value,
139 const char *field_str)
140{
141 const char *handler_name = "define_flag_value";
142 PyObject *t;
143 unsigned long long value;
144 unsigned n = 0;
145
146 if (field_type == PRINT_SYMBOL)
147 handler_name = "define_symbolic_value";
148
149 t = PyTuple_New(4);
150 if (!t)
151 Py_FatalError("couldn't create Python tuple");
152
153 value = eval_flag(field_value);
154
155 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
156 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
157 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
158 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
159
160 try_call_object(handler_name, t);
161
162 Py_DECREF(t);
163}
164
165static void define_values(enum print_arg_type field_type,
166 struct print_flag_sym *field,
167 const char *ev_name,
168 const char *field_name)
169{
170 define_value(field_type, ev_name, field_name, field->value,
171 field->str);
172
173 if (field->next)
174 define_values(field_type, field->next, ev_name, field_name);
175}
176
177static void define_field(enum print_arg_type field_type,
178 const char *ev_name,
179 const char *field_name,
180 const char *delim)
181{
182 const char *handler_name = "define_flag_field";
183 PyObject *t;
184 unsigned n = 0;
185
186 if (field_type == PRINT_SYMBOL)
187 handler_name = "define_symbolic_field";
188
189 if (field_type == PRINT_FLAGS)
190 t = PyTuple_New(3);
191 else
192 t = PyTuple_New(2);
193 if (!t)
194 Py_FatalError("couldn't create Python tuple");
195
196 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
197 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
198 if (field_type == PRINT_FLAGS)
199 PyTuple_SetItem(t, n++, PyString_FromString(delim));
200
201 try_call_object(handler_name, t);
202
203 Py_DECREF(t);
204}
205
206static void define_event_symbols(struct event_format *event,
207 const char *ev_name,
208 struct print_arg *args)
209{
210 if (args == NULL)
211 return;
212
213 switch (args->type) {
214 case PRINT_NULL:
215 break;
216 case PRINT_ATOM:
217 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
218 args->atom.atom);
219 zero_flag_atom = 0;
220 break;
221 case PRINT_FIELD:
222 free(cur_field_name);
223 cur_field_name = strdup(args->field.name);
224 break;
225 case PRINT_FLAGS:
226 define_event_symbols(event, ev_name, args->flags.field);
227 define_field(PRINT_FLAGS, ev_name, cur_field_name,
228 args->flags.delim);
229 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
230 cur_field_name);
231 break;
232 case PRINT_SYMBOL:
233 define_event_symbols(event, ev_name, args->symbol.field);
234 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
235 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
236 cur_field_name);
237 break;
238 case PRINT_HEX:
239 case PRINT_HEX_STR:
240 define_event_symbols(event, ev_name, args->hex.field);
241 define_event_symbols(event, ev_name, args->hex.size);
242 break;
243 case PRINT_INT_ARRAY:
244 define_event_symbols(event, ev_name, args->int_array.field);
245 define_event_symbols(event, ev_name, args->int_array.count);
246 define_event_symbols(event, ev_name, args->int_array.el_size);
247 break;
248 case PRINT_STRING:
249 break;
250 case PRINT_TYPE:
251 define_event_symbols(event, ev_name, args->typecast.item);
252 break;
253 case PRINT_OP:
254 if (strcmp(args->op.op, ":") == 0)
255 zero_flag_atom = 1;
256 define_event_symbols(event, ev_name, args->op.left);
257 define_event_symbols(event, ev_name, args->op.right);
258 break;
259 default:
260 /* gcc warns for these? */
261 case PRINT_BSTRING:
262 case PRINT_DYNAMIC_ARRAY:
263 case PRINT_DYNAMIC_ARRAY_LEN:
264 case PRINT_FUNC:
265 case PRINT_BITMASK:
266 /* we should warn... */
267 return;
268 }
269
270 if (args->next)
271 define_event_symbols(event, ev_name, args->next);
272}
273
274static PyObject *get_field_numeric_entry(struct event_format *event,
275 struct format_field *field, void *data)
276{
277 bool is_array = field->flags & FIELD_IS_ARRAY;
278 PyObject *obj = NULL, *list = NULL;
279 unsigned long long val;
280 unsigned int item_size, n_items, i;
281
282 if (is_array) {
283 list = PyList_New(field->arraylen);
284 item_size = field->size / field->arraylen;
285 n_items = field->arraylen;
286 } else {
287 item_size = field->size;
288 n_items = 1;
289 }
290
291 for (i = 0; i < n_items; i++) {
292
293 val = read_size(event, data + field->offset + i * item_size,
294 item_size);
295 if (field->flags & FIELD_IS_SIGNED) {
296 if ((long long)val >= LONG_MIN &&
297 (long long)val <= LONG_MAX)
298 obj = PyInt_FromLong(val);
299 else
300 obj = PyLong_FromLongLong(val);
301 } else {
302 if (val <= LONG_MAX)
303 obj = PyInt_FromLong(val);
304 else
305 obj = PyLong_FromUnsignedLongLong(val);
306 }
307 if (is_array)
308 PyList_SET_ITEM(list, i, obj);
309 }
310 if (is_array)
311 obj = list;
312 return obj;
313}
314
315
316static PyObject *python_process_callchain(struct perf_sample *sample,
317 struct perf_evsel *evsel,
318 struct addr_location *al)
319{
320 PyObject *pylist;
321
322 pylist = PyList_New(0);
323 if (!pylist)
324 Py_FatalError("couldn't create Python list");
325
326 if (!symbol_conf.use_callchain || !sample->callchain)
327 goto exit;
328
329 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
330 sample, NULL, NULL,
331 scripting_max_stack) != 0) {
332 pr_err("Failed to resolve callchain. Skipping\n");
333 goto exit;
334 }
335 callchain_cursor_commit(&callchain_cursor);
336
337
338 while (1) {
339 PyObject *pyelem;
340 struct callchain_cursor_node *node;
341 node = callchain_cursor_current(&callchain_cursor);
342 if (!node)
343 break;
344
345 pyelem = PyDict_New();
346 if (!pyelem)
347 Py_FatalError("couldn't create Python dictionary");
348
349
350 pydict_set_item_string_decref(pyelem, "ip",
351 PyLong_FromUnsignedLongLong(node->ip));
352
353 if (node->sym) {
354 PyObject *pysym = PyDict_New();
355 if (!pysym)
356 Py_FatalError("couldn't create Python dictionary");
357 pydict_set_item_string_decref(pysym, "start",
358 PyLong_FromUnsignedLongLong(node->sym->start));
359 pydict_set_item_string_decref(pysym, "end",
360 PyLong_FromUnsignedLongLong(node->sym->end));
361 pydict_set_item_string_decref(pysym, "binding",
362 PyInt_FromLong(node->sym->binding));
363 pydict_set_item_string_decref(pysym, "name",
364 PyString_FromStringAndSize(node->sym->name,
365 node->sym->namelen));
366 pydict_set_item_string_decref(pyelem, "sym", pysym);
367 }
368
369 if (node->map) {
370 struct map *map = node->map;
371 const char *dsoname = "[unknown]";
372 if (map && map->dso) {
373 if (symbol_conf.show_kernel_path && map->dso->long_name)
374 dsoname = map->dso->long_name;
375 else
376 dsoname = map->dso->name;
377 }
378 pydict_set_item_string_decref(pyelem, "dso",
379 PyString_FromString(dsoname));
380 }
381
382 callchain_cursor_advance(&callchain_cursor);
383 PyList_Append(pylist, pyelem);
384 Py_DECREF(pyelem);
385 }
386
387exit:
388 return pylist;
389}
390
391static void python_process_tracepoint(struct perf_sample *sample,
392 struct perf_evsel *evsel,
393 struct addr_location *al)
394{
395 struct event_format *event = evsel->tp_format;
396 PyObject *handler, *context, *t, *obj = NULL, *callchain;
397 PyObject *dict = NULL;
398 static char handler_name[256];
399 struct format_field *field;
400 unsigned long s, ns;
401 unsigned n = 0;
402 int pid;
403 int cpu = sample->cpu;
404 void *data = sample->raw_data;
405 unsigned long long nsecs = sample->time;
406 const char *comm = thread__comm_str(al->thread);
407
408 t = PyTuple_New(MAX_FIELDS);
409 if (!t)
410 Py_FatalError("couldn't create Python tuple");
411
412 if (!event) {
413 snprintf(handler_name, sizeof(handler_name),
414 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
415 Py_FatalError(handler_name);
416 }
417
418 pid = raw_field_value(event, "common_pid", data);
419
420 sprintf(handler_name, "%s__%s", event->system, event->name);
421
422 if (!test_and_set_bit(event->id, events_defined))
423 define_event_symbols(event, handler_name, event->print_fmt.args);
424
425 handler = get_handler(handler_name);
426 if (!handler) {
427 dict = PyDict_New();
428 if (!dict)
429 Py_FatalError("couldn't create Python dict");
430 }
431 s = nsecs / NSEC_PER_SEC;
432 ns = nsecs - s * NSEC_PER_SEC;
433
434 scripting_context->event_data = data;
435 scripting_context->pevent = evsel->tp_format->pevent;
436
437 context = PyCObject_FromVoidPtr(scripting_context, NULL);
438
439 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
440 PyTuple_SetItem(t, n++, context);
441
442 /* ip unwinding */
443 callchain = python_process_callchain(sample, evsel, al);
444
445 if (handler) {
446 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
447 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
448 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
449 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
450 PyTuple_SetItem(t, n++, PyString_FromString(comm));
451 PyTuple_SetItem(t, n++, callchain);
452 } else {
453 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
454 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
455 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
456 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
457 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
458 pydict_set_item_string_decref(dict, "common_callchain", callchain);
459 }
460 for (field = event->format.fields; field; field = field->next) {
461 unsigned int offset, len;
462 unsigned long long val;
463
464 if (field->flags & FIELD_IS_ARRAY) {
465 offset = field->offset;
466 len = field->size;
467 if (field->flags & FIELD_IS_DYNAMIC) {
468 val = pevent_read_number(scripting_context->pevent,
469 data + offset, len);
470 offset = val;
471 len = offset >> 16;
472 offset &= 0xffff;
473 }
474 if (field->flags & FIELD_IS_STRING &&
475 is_printable_array(data + offset, len)) {
476 obj = PyString_FromString((char *) data + offset);
477 } else {
478 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
479 field->flags &= ~FIELD_IS_STRING;
480 }
481 } else { /* FIELD_IS_NUMERIC */
482 obj = get_field_numeric_entry(event, field, data);
483 }
484 if (handler)
485 PyTuple_SetItem(t, n++, obj);
486 else
487 pydict_set_item_string_decref(dict, field->name, obj);
488
489 }
490
491 if (!handler)
492 PyTuple_SetItem(t, n++, dict);
493
494 if (_PyTuple_Resize(&t, n) == -1)
495 Py_FatalError("error resizing Python tuple");
496
497 if (handler) {
498 call_object(handler, t, handler_name);
499 } else {
500 try_call_object("trace_unhandled", t);
501 Py_DECREF(dict);
502 }
503
504 Py_DECREF(t);
505}
506
507static PyObject *tuple_new(unsigned int sz)
508{
509 PyObject *t;
510
511 t = PyTuple_New(sz);
512 if (!t)
513 Py_FatalError("couldn't create Python tuple");
514 return t;
515}
516
517static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
518{
519#if BITS_PER_LONG == 64
520 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
521#endif
522#if BITS_PER_LONG == 32
523 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
524#endif
525}
526
527static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
528{
529 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
530}
531
532static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
533{
534 return PyTuple_SetItem(t, pos, PyString_FromString(s));
535}
536
537static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
538{
539 struct tables *tables = container_of(dbe, struct tables, dbe);
540 PyObject *t;
541
542 t = tuple_new(2);
543
544 tuple_set_u64(t, 0, evsel->db_id);
545 tuple_set_string(t, 1, perf_evsel__name(evsel));
546
547 call_object(tables->evsel_handler, t, "evsel_table");
548
549 Py_DECREF(t);
550
551 return 0;
552}
553
554static int python_export_machine(struct db_export *dbe,
555 struct machine *machine)
556{
557 struct tables *tables = container_of(dbe, struct tables, dbe);
558 PyObject *t;
559
560 t = tuple_new(3);
561
562 tuple_set_u64(t, 0, machine->db_id);
563 tuple_set_s32(t, 1, machine->pid);
564 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
565
566 call_object(tables->machine_handler, t, "machine_table");
567
568 Py_DECREF(t);
569
570 return 0;
571}
572
573static int python_export_thread(struct db_export *dbe, struct thread *thread,
574 u64 main_thread_db_id, struct machine *machine)
575{
576 struct tables *tables = container_of(dbe, struct tables, dbe);
577 PyObject *t;
578
579 t = tuple_new(5);
580
581 tuple_set_u64(t, 0, thread->db_id);
582 tuple_set_u64(t, 1, machine->db_id);
583 tuple_set_u64(t, 2, main_thread_db_id);
584 tuple_set_s32(t, 3, thread->pid_);
585 tuple_set_s32(t, 4, thread->tid);
586
587 call_object(tables->thread_handler, t, "thread_table");
588
589 Py_DECREF(t);
590
591 return 0;
592}
593
594static int python_export_comm(struct db_export *dbe, struct comm *comm)
595{
596 struct tables *tables = container_of(dbe, struct tables, dbe);
597 PyObject *t;
598
599 t = tuple_new(2);
600
601 tuple_set_u64(t, 0, comm->db_id);
602 tuple_set_string(t, 1, comm__str(comm));
603
604 call_object(tables->comm_handler, t, "comm_table");
605
606 Py_DECREF(t);
607
608 return 0;
609}
610
611static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
612 struct comm *comm, struct thread *thread)
613{
614 struct tables *tables = container_of(dbe, struct tables, dbe);
615 PyObject *t;
616
617 t = tuple_new(3);
618
619 tuple_set_u64(t, 0, db_id);
620 tuple_set_u64(t, 1, comm->db_id);
621 tuple_set_u64(t, 2, thread->db_id);
622
623 call_object(tables->comm_thread_handler, t, "comm_thread_table");
624
625 Py_DECREF(t);
626
627 return 0;
628}
629
630static int python_export_dso(struct db_export *dbe, struct dso *dso,
631 struct machine *machine)
632{
633 struct tables *tables = container_of(dbe, struct tables, dbe);
634 char sbuild_id[SBUILD_ID_SIZE];
635 PyObject *t;
636
637 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
638
639 t = tuple_new(5);
640
641 tuple_set_u64(t, 0, dso->db_id);
642 tuple_set_u64(t, 1, machine->db_id);
643 tuple_set_string(t, 2, dso->short_name);
644 tuple_set_string(t, 3, dso->long_name);
645 tuple_set_string(t, 4, sbuild_id);
646
647 call_object(tables->dso_handler, t, "dso_table");
648
649 Py_DECREF(t);
650
651 return 0;
652}
653
654static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
655 struct dso *dso)
656{
657 struct tables *tables = container_of(dbe, struct tables, dbe);
658 u64 *sym_db_id = symbol__priv(sym);
659 PyObject *t;
660
661 t = tuple_new(6);
662
663 tuple_set_u64(t, 0, *sym_db_id);
664 tuple_set_u64(t, 1, dso->db_id);
665 tuple_set_u64(t, 2, sym->start);
666 tuple_set_u64(t, 3, sym->end);
667 tuple_set_s32(t, 4, sym->binding);
668 tuple_set_string(t, 5, sym->name);
669
670 call_object(tables->symbol_handler, t, "symbol_table");
671
672 Py_DECREF(t);
673
674 return 0;
675}
676
677static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
678 const char *name)
679{
680 struct tables *tables = container_of(dbe, struct tables, dbe);
681 PyObject *t;
682
683 t = tuple_new(2);
684
685 tuple_set_s32(t, 0, branch_type);
686 tuple_set_string(t, 1, name);
687
688 call_object(tables->branch_type_handler, t, "branch_type_table");
689
690 Py_DECREF(t);
691
692 return 0;
693}
694
695static int python_export_sample(struct db_export *dbe,
696 struct export_sample *es)
697{
698 struct tables *tables = container_of(dbe, struct tables, dbe);
699 PyObject *t;
700
701 t = tuple_new(22);
702
703 tuple_set_u64(t, 0, es->db_id);
704 tuple_set_u64(t, 1, es->evsel->db_id);
705 tuple_set_u64(t, 2, es->al->machine->db_id);
706 tuple_set_u64(t, 3, es->al->thread->db_id);
707 tuple_set_u64(t, 4, es->comm_db_id);
708 tuple_set_u64(t, 5, es->dso_db_id);
709 tuple_set_u64(t, 6, es->sym_db_id);
710 tuple_set_u64(t, 7, es->offset);
711 tuple_set_u64(t, 8, es->sample->ip);
712 tuple_set_u64(t, 9, es->sample->time);
713 tuple_set_s32(t, 10, es->sample->cpu);
714 tuple_set_u64(t, 11, es->addr_dso_db_id);
715 tuple_set_u64(t, 12, es->addr_sym_db_id);
716 tuple_set_u64(t, 13, es->addr_offset);
717 tuple_set_u64(t, 14, es->sample->addr);
718 tuple_set_u64(t, 15, es->sample->period);
719 tuple_set_u64(t, 16, es->sample->weight);
720 tuple_set_u64(t, 17, es->sample->transaction);
721 tuple_set_u64(t, 18, es->sample->data_src);
722 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
723 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
724 tuple_set_u64(t, 21, es->call_path_id);
725
726 call_object(tables->sample_handler, t, "sample_table");
727
728 Py_DECREF(t);
729
730 return 0;
731}
732
733static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
734{
735 struct tables *tables = container_of(dbe, struct tables, dbe);
736 PyObject *t;
737 u64 parent_db_id, sym_db_id;
738
739 parent_db_id = cp->parent ? cp->parent->db_id : 0;
740 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
741
742 t = tuple_new(4);
743
744 tuple_set_u64(t, 0, cp->db_id);
745 tuple_set_u64(t, 1, parent_db_id);
746 tuple_set_u64(t, 2, sym_db_id);
747 tuple_set_u64(t, 3, cp->ip);
748
749 call_object(tables->call_path_handler, t, "call_path_table");
750
751 Py_DECREF(t);
752
753 return 0;
754}
755
756static int python_export_call_return(struct db_export *dbe,
757 struct call_return *cr)
758{
759 struct tables *tables = container_of(dbe, struct tables, dbe);
760 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
761 PyObject *t;
762
763 t = tuple_new(11);
764
765 tuple_set_u64(t, 0, cr->db_id);
766 tuple_set_u64(t, 1, cr->thread->db_id);
767 tuple_set_u64(t, 2, comm_db_id);
768 tuple_set_u64(t, 3, cr->cp->db_id);
769 tuple_set_u64(t, 4, cr->call_time);
770 tuple_set_u64(t, 5, cr->return_time);
771 tuple_set_u64(t, 6, cr->branch_count);
772 tuple_set_u64(t, 7, cr->call_ref);
773 tuple_set_u64(t, 8, cr->return_ref);
774 tuple_set_u64(t, 9, cr->cp->parent->db_id);
775 tuple_set_s32(t, 10, cr->flags);
776
777 call_object(tables->call_return_handler, t, "call_return_table");
778
779 Py_DECREF(t);
780
781 return 0;
782}
783
784static int python_process_call_return(struct call_return *cr, void *data)
785{
786 struct db_export *dbe = data;
787
788 return db_export__call_return(dbe, cr);
789}
790
791static void python_process_general_event(struct perf_sample *sample,
792 struct perf_evsel *evsel,
793 struct addr_location *al)
794{
795 PyObject *handler, *t, *dict, *callchain, *dict_sample;
796 static char handler_name[64];
797 unsigned n = 0;
798
799 /*
800 * Use the MAX_FIELDS to make the function expandable, though
801 * currently there is only one item for the tuple.
802 */
803 t = PyTuple_New(MAX_FIELDS);
804 if (!t)
805 Py_FatalError("couldn't create Python tuple");
806
807 dict = PyDict_New();
808 if (!dict)
809 Py_FatalError("couldn't create Python dictionary");
810
811 dict_sample = PyDict_New();
812 if (!dict_sample)
813 Py_FatalError("couldn't create Python dictionary");
814
815 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
816
817 handler = get_handler(handler_name);
818 if (!handler)
819 goto exit;
820
821 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
822 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
823 (const char *)&evsel->attr, sizeof(evsel->attr)));
824
825 pydict_set_item_string_decref(dict_sample, "pid",
826 PyInt_FromLong(sample->pid));
827 pydict_set_item_string_decref(dict_sample, "tid",
828 PyInt_FromLong(sample->tid));
829 pydict_set_item_string_decref(dict_sample, "cpu",
830 PyInt_FromLong(sample->cpu));
831 pydict_set_item_string_decref(dict_sample, "ip",
832 PyLong_FromUnsignedLongLong(sample->ip));
833 pydict_set_item_string_decref(dict_sample, "time",
834 PyLong_FromUnsignedLongLong(sample->time));
835 pydict_set_item_string_decref(dict_sample, "period",
836 PyLong_FromUnsignedLongLong(sample->period));
837 pydict_set_item_string_decref(dict, "sample", dict_sample);
838
839 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
840 (const char *)sample->raw_data, sample->raw_size));
841 pydict_set_item_string_decref(dict, "comm",
842 PyString_FromString(thread__comm_str(al->thread)));
843 if (al->map) {
844 pydict_set_item_string_decref(dict, "dso",
845 PyString_FromString(al->map->dso->name));
846 }
847 if (al->sym) {
848 pydict_set_item_string_decref(dict, "symbol",
849 PyString_FromString(al->sym->name));
850 }
851
852 /* ip unwinding */
853 callchain = python_process_callchain(sample, evsel, al);
854 pydict_set_item_string_decref(dict, "callchain", callchain);
855
856 PyTuple_SetItem(t, n++, dict);
857 if (_PyTuple_Resize(&t, n) == -1)
858 Py_FatalError("error resizing Python tuple");
859
860 call_object(handler, t, handler_name);
861exit:
862 Py_DECREF(dict);
863 Py_DECREF(t);
864}
865
866static void python_process_event(union perf_event *event,
867 struct perf_sample *sample,
868 struct perf_evsel *evsel,
869 struct addr_location *al)
870{
871 struct tables *tables = &tables_global;
872
873 switch (evsel->attr.type) {
874 case PERF_TYPE_TRACEPOINT:
875 python_process_tracepoint(sample, evsel, al);
876 break;
877 /* Reserve for future process_hw/sw/raw APIs */
878 default:
879 if (tables->db_export_mode)
880 db_export__sample(&tables->dbe, event, sample, evsel, al);
881 else
882 python_process_general_event(sample, evsel, al);
883 }
884}
885
886static void get_handler_name(char *str, size_t size,
887 struct perf_evsel *evsel)
888{
889 char *p = str;
890
891 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
892
893 while ((p = strchr(p, ':'))) {
894 *p = '_';
895 p++;
896 }
897}
898
899static void
900process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
901 struct perf_counts_values *count)
902{
903 PyObject *handler, *t;
904 static char handler_name[256];
905 int n = 0;
906
907 t = PyTuple_New(MAX_FIELDS);
908 if (!t)
909 Py_FatalError("couldn't create Python tuple");
910
911 get_handler_name(handler_name, sizeof(handler_name),
912 counter);
913
914 handler = get_handler(handler_name);
915 if (!handler) {
916 pr_debug("can't find python handler %s\n", handler_name);
917 return;
918 }
919
920 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
921 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
922
923 tuple_set_u64(t, n++, tstamp);
924 tuple_set_u64(t, n++, count->val);
925 tuple_set_u64(t, n++, count->ena);
926 tuple_set_u64(t, n++, count->run);
927
928 if (_PyTuple_Resize(&t, n) == -1)
929 Py_FatalError("error resizing Python tuple");
930
931 call_object(handler, t, handler_name);
932
933 Py_DECREF(t);
934}
935
936static void python_process_stat(struct perf_stat_config *config,
937 struct perf_evsel *counter, u64 tstamp)
938{
939 struct thread_map *threads = counter->threads;
940 struct cpu_map *cpus = counter->cpus;
941 int cpu, thread;
942
943 if (config->aggr_mode == AGGR_GLOBAL) {
944 process_stat(counter, -1, -1, tstamp,
945 &counter->counts->aggr);
946 return;
947 }
948
949 for (thread = 0; thread < threads->nr; thread++) {
950 for (cpu = 0; cpu < cpus->nr; cpu++) {
951 process_stat(counter, cpus->map[cpu],
952 thread_map__pid(threads, thread), tstamp,
953 perf_counts(counter->counts, cpu, thread));
954 }
955 }
956}
957
958static void python_process_stat_interval(u64 tstamp)
959{
960 PyObject *handler, *t;
961 static const char handler_name[] = "stat__interval";
962 int n = 0;
963
964 t = PyTuple_New(MAX_FIELDS);
965 if (!t)
966 Py_FatalError("couldn't create Python tuple");
967
968 handler = get_handler(handler_name);
969 if (!handler) {
970 pr_debug("can't find python handler %s\n", handler_name);
971 return;
972 }
973
974 tuple_set_u64(t, n++, tstamp);
975
976 if (_PyTuple_Resize(&t, n) == -1)
977 Py_FatalError("error resizing Python tuple");
978
979 call_object(handler, t, handler_name);
980
981 Py_DECREF(t);
982}
983
984static int run_start_sub(void)
985{
986 main_module = PyImport_AddModule("__main__");
987 if (main_module == NULL)
988 return -1;
989 Py_INCREF(main_module);
990
991 main_dict = PyModule_GetDict(main_module);
992 if (main_dict == NULL)
993 goto error;
994 Py_INCREF(main_dict);
995
996 try_call_object("trace_begin", NULL);
997
998 return 0;
999
1000error:
1001 Py_XDECREF(main_dict);
1002 Py_XDECREF(main_module);
1003 return -1;
1004}
1005
1006#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1007 tables->handler_name = get_handler(#table_name); \
1008 if (tables->handler_name) \
1009 tables->dbe.export_ ## name = python_export_ ## name; \
1010} while (0)
1011
1012#define SET_TABLE_HANDLER(name) \
1013 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1014
1015static void set_table_handlers(struct tables *tables)
1016{
1017 const char *perf_db_export_mode = "perf_db_export_mode";
1018 const char *perf_db_export_calls = "perf_db_export_calls";
1019 const char *perf_db_export_callchains = "perf_db_export_callchains";
1020 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1021 bool export_calls = false;
1022 bool export_callchains = false;
1023 int ret;
1024
1025 memset(tables, 0, sizeof(struct tables));
1026 if (db_export__init(&tables->dbe))
1027 Py_FatalError("failed to initialize export");
1028
1029 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1030 if (!db_export_mode)
1031 return;
1032
1033 ret = PyObject_IsTrue(db_export_mode);
1034 if (ret == -1)
1035 handler_call_die(perf_db_export_mode);
1036 if (!ret)
1037 return;
1038
1039 /* handle export calls */
1040 tables->dbe.crp = NULL;
1041 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1042 if (db_export_calls) {
1043 ret = PyObject_IsTrue(db_export_calls);
1044 if (ret == -1)
1045 handler_call_die(perf_db_export_calls);
1046 export_calls = !!ret;
1047 }
1048
1049 if (export_calls) {
1050 tables->dbe.crp =
1051 call_return_processor__new(python_process_call_return,
1052 &tables->dbe);
1053 if (!tables->dbe.crp)
1054 Py_FatalError("failed to create calls processor");
1055 }
1056
1057 /* handle export callchains */
1058 tables->dbe.cpr = NULL;
1059 db_export_callchains = PyDict_GetItemString(main_dict,
1060 perf_db_export_callchains);
1061 if (db_export_callchains) {
1062 ret = PyObject_IsTrue(db_export_callchains);
1063 if (ret == -1)
1064 handler_call_die(perf_db_export_callchains);
1065 export_callchains = !!ret;
1066 }
1067
1068 if (export_callchains) {
1069 /*
1070 * Attempt to use the call path root from the call return
1071 * processor, if the call return processor is in use. Otherwise,
1072 * we allocate a new call path root. This prevents exporting
1073 * duplicate call path ids when both are in use simultaniously.
1074 */
1075 if (tables->dbe.crp)
1076 tables->dbe.cpr = tables->dbe.crp->cpr;
1077 else
1078 tables->dbe.cpr = call_path_root__new();
1079
1080 if (!tables->dbe.cpr)
1081 Py_FatalError("failed to create call path root");
1082 }
1083
1084 tables->db_export_mode = true;
1085 /*
1086 * Reserve per symbol space for symbol->db_id via symbol__priv()
1087 */
1088 symbol_conf.priv_size = sizeof(u64);
1089
1090 SET_TABLE_HANDLER(evsel);
1091 SET_TABLE_HANDLER(machine);
1092 SET_TABLE_HANDLER(thread);
1093 SET_TABLE_HANDLER(comm);
1094 SET_TABLE_HANDLER(comm_thread);
1095 SET_TABLE_HANDLER(dso);
1096 SET_TABLE_HANDLER(symbol);
1097 SET_TABLE_HANDLER(branch_type);
1098 SET_TABLE_HANDLER(sample);
1099 SET_TABLE_HANDLER(call_path);
1100 SET_TABLE_HANDLER(call_return);
1101}
1102
1103/*
1104 * Start trace script
1105 */
1106static int python_start_script(const char *script, int argc, const char **argv)
1107{
1108 struct tables *tables = &tables_global;
1109 const char **command_line;
1110 char buf[PATH_MAX];
1111 int i, err = 0;
1112 FILE *fp;
1113
1114 command_line = malloc((argc + 1) * sizeof(const char *));
1115 command_line[0] = script;
1116 for (i = 1; i < argc + 1; i++)
1117 command_line[i] = argv[i - 1];
1118
1119 Py_Initialize();
1120
1121 initperf_trace_context();
1122
1123 PySys_SetArgv(argc + 1, (char **)command_line);
1124
1125 fp = fopen(script, "r");
1126 if (!fp) {
1127 sprintf(buf, "Can't open python script \"%s\"", script);
1128 perror(buf);
1129 err = -1;
1130 goto error;
1131 }
1132
1133 err = PyRun_SimpleFile(fp, script);
1134 if (err) {
1135 fprintf(stderr, "Error running python script %s\n", script);
1136 goto error;
1137 }
1138
1139 err = run_start_sub();
1140 if (err) {
1141 fprintf(stderr, "Error starting python script %s\n", script);
1142 goto error;
1143 }
1144
1145 set_table_handlers(tables);
1146
1147 if (tables->db_export_mode) {
1148 err = db_export__branch_types(&tables->dbe);
1149 if (err)
1150 goto error;
1151 }
1152
1153 free(command_line);
1154
1155 return err;
1156error:
1157 Py_Finalize();
1158 free(command_line);
1159
1160 return err;
1161}
1162
1163static int python_flush_script(void)
1164{
1165 struct tables *tables = &tables_global;
1166
1167 return db_export__flush(&tables->dbe);
1168}
1169
1170/*
1171 * Stop trace script
1172 */
1173static int python_stop_script(void)
1174{
1175 struct tables *tables = &tables_global;
1176
1177 try_call_object("trace_end", NULL);
1178
1179 db_export__exit(&tables->dbe);
1180
1181 Py_XDECREF(main_dict);
1182 Py_XDECREF(main_module);
1183 Py_Finalize();
1184
1185 return 0;
1186}
1187
1188static int python_generate_script(struct pevent *pevent, const char *outfile)
1189{
1190 struct event_format *event = NULL;
1191 struct format_field *f;
1192 char fname[PATH_MAX];
1193 int not_first, count;
1194 FILE *ofp;
1195
1196 sprintf(fname, "%s.py", outfile);
1197 ofp = fopen(fname, "w");
1198 if (ofp == NULL) {
1199 fprintf(stderr, "couldn't open %s\n", fname);
1200 return -1;
1201 }
1202 fprintf(ofp, "# perf script event handlers, "
1203 "generated by perf script -g python\n");
1204
1205 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1206 " License version 2\n\n");
1207
1208 fprintf(ofp, "# The common_* event handler fields are the most useful "
1209 "fields common to\n");
1210
1211 fprintf(ofp, "# all events. They don't necessarily correspond to "
1212 "the 'common_*' fields\n");
1213
1214 fprintf(ofp, "# in the format files. Those fields not available as "
1215 "handler params can\n");
1216
1217 fprintf(ofp, "# be retrieved using Python functions of the form "
1218 "common_*(context).\n");
1219
1220 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1221 "of available functions.\n\n");
1222
1223 fprintf(ofp, "import os\n");
1224 fprintf(ofp, "import sys\n\n");
1225
1226 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1227 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1228 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1229 fprintf(ofp, "from Core import *\n\n\n");
1230
1231 fprintf(ofp, "def trace_begin():\n");
1232 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1233
1234 fprintf(ofp, "def trace_end():\n");
1235 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1236
1237 while ((event = trace_find_next_event(pevent, event))) {
1238 fprintf(ofp, "def %s__%s(", event->system, event->name);
1239 fprintf(ofp, "event_name, ");
1240 fprintf(ofp, "context, ");
1241 fprintf(ofp, "common_cpu,\n");
1242 fprintf(ofp, "\tcommon_secs, ");
1243 fprintf(ofp, "common_nsecs, ");
1244 fprintf(ofp, "common_pid, ");
1245 fprintf(ofp, "common_comm,\n\t");
1246 fprintf(ofp, "common_callchain, ");
1247
1248 not_first = 0;
1249 count = 0;
1250
1251 for (f = event->format.fields; f; f = f->next) {
1252 if (not_first++)
1253 fprintf(ofp, ", ");
1254 if (++count % 5 == 0)
1255 fprintf(ofp, "\n\t");
1256
1257 fprintf(ofp, "%s", f->name);
1258 }
1259 fprintf(ofp, "):\n");
1260
1261 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1262 "common_secs, common_nsecs,\n\t\t\t"
1263 "common_pid, common_comm)\n\n");
1264
1265 fprintf(ofp, "\t\tprint \"");
1266
1267 not_first = 0;
1268 count = 0;
1269
1270 for (f = event->format.fields; f; f = f->next) {
1271 if (not_first++)
1272 fprintf(ofp, ", ");
1273 if (count && count % 3 == 0) {
1274 fprintf(ofp, "\" \\\n\t\t\"");
1275 }
1276 count++;
1277
1278 fprintf(ofp, "%s=", f->name);
1279 if (f->flags & FIELD_IS_STRING ||
1280 f->flags & FIELD_IS_FLAG ||
1281 f->flags & FIELD_IS_ARRAY ||
1282 f->flags & FIELD_IS_SYMBOLIC)
1283 fprintf(ofp, "%%s");
1284 else if (f->flags & FIELD_IS_SIGNED)
1285 fprintf(ofp, "%%d");
1286 else
1287 fprintf(ofp, "%%u");
1288 }
1289
1290 fprintf(ofp, "\" %% \\\n\t\t(");
1291
1292 not_first = 0;
1293 count = 0;
1294
1295 for (f = event->format.fields; f; f = f->next) {
1296 if (not_first++)
1297 fprintf(ofp, ", ");
1298
1299 if (++count % 5 == 0)
1300 fprintf(ofp, "\n\t\t");
1301
1302 if (f->flags & FIELD_IS_FLAG) {
1303 if ((count - 1) % 5 != 0) {
1304 fprintf(ofp, "\n\t\t");
1305 count = 4;
1306 }
1307 fprintf(ofp, "flag_str(\"");
1308 fprintf(ofp, "%s__%s\", ", event->system,
1309 event->name);
1310 fprintf(ofp, "\"%s\", %s)", f->name,
1311 f->name);
1312 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1313 if ((count - 1) % 5 != 0) {
1314 fprintf(ofp, "\n\t\t");
1315 count = 4;
1316 }
1317 fprintf(ofp, "symbol_str(\"");
1318 fprintf(ofp, "%s__%s\", ", event->system,
1319 event->name);
1320 fprintf(ofp, "\"%s\", %s)", f->name,
1321 f->name);
1322 } else
1323 fprintf(ofp, "%s", f->name);
1324 }
1325
1326 fprintf(ofp, ")\n\n");
1327
1328 fprintf(ofp, "\t\tfor node in common_callchain:");
1329 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1330 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1331 fprintf(ofp, "\n\t\t\telse:");
1332 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1333 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1334
1335 }
1336
1337 fprintf(ofp, "def trace_unhandled(event_name, context, "
1338 "event_fields_dict):\n");
1339
1340 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1341 "for k,v in sorted(event_fields_dict.items())])\n\n");
1342
1343 fprintf(ofp, "def print_header("
1344 "event_name, cpu, secs, nsecs, pid, comm):\n"
1345 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1346 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1347
1348 fclose(ofp);
1349
1350 fprintf(stderr, "generated Python script: %s\n", fname);
1351
1352 return 0;
1353}
1354
1355struct scripting_ops python_scripting_ops = {
1356 .name = "Python",
1357 .start_script = python_start_script,
1358 .flush_script = python_flush_script,
1359 .stop_script = python_stop_script,
1360 .process_event = python_process_event,
1361 .process_stat = python_process_stat,
1362 .process_stat_interval = python_process_stat_interval,
1363 .generate_script = python_generate_script,
1364};