1 /*
2  * trace-event-python.  Feed trace events to an embedded Python interpreter.
3  *
4  * Copyright (C) 2010 Tom Zanussi <[email protected]>
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 <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>
33 
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 "map.h"
48 #include "thread_map.h"
49 #include "cpumap.h"
50 #include "print_binary.h"
51 #include "stat.h"
52 #include "mem-events.h"
53 
54 #if PY_MAJOR_VERSION < 3
55 #define _PyUnicode_FromString(arg) \
56   PyString_FromString(arg)
57 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
58   PyString_FromStringAndSize((arg1), (arg2))
59 #define _PyBytes_FromStringAndSize(arg1, arg2) \
60   PyString_FromStringAndSize((arg1), (arg2))
61 #define _PyLong_FromLong(arg) \
62   PyInt_FromLong(arg)
63 #define _PyLong_AsLong(arg) \
64   PyInt_AsLong(arg)
65 #define _PyCapsule_New(arg1, arg2, arg3) \
66   PyCObject_FromVoidPtr((arg1), (arg2))
67 
68 PyMODINIT_FUNC initperf_trace_context(void);
69 #else
70 #define _PyUnicode_FromString(arg) \
71   PyUnicode_FromString(arg)
72 #define _PyUnicode_FromStringAndSize(arg1, arg2) \
73   PyUnicode_FromStringAndSize((arg1), (arg2))
74 #define _PyBytes_FromStringAndSize(arg1, arg2) \
75   PyBytes_FromStringAndSize((arg1), (arg2))
76 #define _PyLong_FromLong(arg) \
77   PyLong_FromLong(arg)
78 #define _PyLong_AsLong(arg) \
79   PyLong_AsLong(arg)
80 #define _PyCapsule_New(arg1, arg2, arg3) \
81   PyCapsule_New((arg1), (arg2), (arg3))
82 
83 PyMODINIT_FUNC PyInit_perf_trace_context(void);
84 #endif
85 
86 #define TRACE_EVENT_TYPE_MAX				\
87 	((1 << (sizeof(unsigned short) * 8)) - 1)
88 
89 static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
90 
91 #define MAX_FIELDS	64
92 #define N_COMMON_FIELDS	7
93 
94 extern struct scripting_context *scripting_context;
95 
96 static char *cur_field_name;
97 static int zero_flag_atom;
98 
99 static PyObject *main_module, *main_dict;
100 
101 struct tables {
102 	struct db_export	dbe;
103 	PyObject		*evsel_handler;
104 	PyObject		*machine_handler;
105 	PyObject		*thread_handler;
106 	PyObject		*comm_handler;
107 	PyObject		*comm_thread_handler;
108 	PyObject		*dso_handler;
109 	PyObject		*symbol_handler;
110 	PyObject		*branch_type_handler;
111 	PyObject		*sample_handler;
112 	PyObject		*call_path_handler;
113 	PyObject		*call_return_handler;
114 	bool			db_export_mode;
115 };
116 
117 static struct tables tables_global;
118 
119 static void handler_call_die(const char *handler_name) __noreturn;
120 static void handler_call_die(const char *handler_name)
121 {
122 	PyErr_Print();
123 	Py_FatalError("problem in Python trace event handler");
124 	// Py_FatalError does not return
125 	// but we have to make the compiler happy
126 	abort();
127 }
128 
129 /*
130  * Insert val into into the dictionary and decrement the reference counter.
131  * This is necessary for dictionaries since PyDict_SetItemString() does not
132  * steal a reference, as opposed to PyTuple_SetItem().
133  */
134 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
135 {
136 	PyDict_SetItemString(dict, key, val);
137 	Py_DECREF(val);
138 }
139 
140 static PyObject *get_handler(const char *handler_name)
141 {
142 	PyObject *handler;
143 
144 	handler = PyDict_GetItemString(main_dict, handler_name);
145 	if (handler && !PyCallable_Check(handler))
146 		return NULL;
147 	return handler;
148 }
149 
150 static int get_argument_count(PyObject *handler)
151 {
152 	int arg_count = 0;
153 
154 	/*
155 	 * The attribute for the code object is func_code in Python 2,
156 	 * whereas it is __code__ in Python 3.0+.
157 	 */
158 	PyObject *code_obj = PyObject_GetAttrString(handler,
159 		"func_code");
160 	if (PyErr_Occurred()) {
161 		PyErr_Clear();
162 		code_obj = PyObject_GetAttrString(handler,
163 			"__code__");
164 	}
165 	PyErr_Clear();
166 	if (code_obj) {
167 		PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
168 			"co_argcount");
169 		if (arg_count_obj) {
170 			arg_count = (int) _PyLong_AsLong(arg_count_obj);
171 			Py_DECREF(arg_count_obj);
172 		}
173 		Py_DECREF(code_obj);
174 	}
175 	return arg_count;
176 }
177 
178 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
179 {
180 	PyObject *retval;
181 
182 	retval = PyObject_CallObject(handler, args);
183 	if (retval == NULL)
184 		handler_call_die(die_msg);
185 	Py_DECREF(retval);
186 }
187 
188 static void try_call_object(const char *handler_name, PyObject *args)
189 {
190 	PyObject *handler;
191 
192 	handler = get_handler(handler_name);
193 	if (handler)
194 		call_object(handler, args, handler_name);
195 }
196 
197 static void define_value(enum tep_print_arg_type field_type,
198 			 const char *ev_name,
199 			 const char *field_name,
200 			 const char *field_value,
201 			 const char *field_str)
202 {
203 	const char *handler_name = "define_flag_value";
204 	PyObject *t;
205 	unsigned long long value;
206 	unsigned n = 0;
207 
208 	if (field_type == TEP_PRINT_SYMBOL)
209 		handler_name = "define_symbolic_value";
210 
211 	t = PyTuple_New(4);
212 	if (!t)
213 		Py_FatalError("couldn't create Python tuple");
214 
215 	value = eval_flag(field_value);
216 
217 	PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
218 	PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
219 	PyTuple_SetItem(t, n++, _PyLong_FromLong(value));
220 	PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str));
221 
222 	try_call_object(handler_name, t);
223 
224 	Py_DECREF(t);
225 }
226 
227 static void define_values(enum tep_print_arg_type field_type,
228 			  struct tep_print_flag_sym *field,
229 			  const char *ev_name,
230 			  const char *field_name)
231 {
232 	define_value(field_type, ev_name, field_name, field->value,
233 		     field->str);
234 
235 	if (field->next)
236 		define_values(field_type, field->next, ev_name, field_name);
237 }
238 
239 static void define_field(enum tep_print_arg_type field_type,
240 			 const char *ev_name,
241 			 const char *field_name,
242 			 const char *delim)
243 {
244 	const char *handler_name = "define_flag_field";
245 	PyObject *t;
246 	unsigned n = 0;
247 
248 	if (field_type == TEP_PRINT_SYMBOL)
249 		handler_name = "define_symbolic_field";
250 
251 	if (field_type == TEP_PRINT_FLAGS)
252 		t = PyTuple_New(3);
253 	else
254 		t = PyTuple_New(2);
255 	if (!t)
256 		Py_FatalError("couldn't create Python tuple");
257 
258 	PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
259 	PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
260 	if (field_type == TEP_PRINT_FLAGS)
261 		PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim));
262 
263 	try_call_object(handler_name, t);
264 
265 	Py_DECREF(t);
266 }
267 
268 static void define_event_symbols(struct tep_event *event,
269 				 const char *ev_name,
270 				 struct tep_print_arg *args)
271 {
272 	if (args == NULL)
273 		return;
274 
275 	switch (args->type) {
276 	case TEP_PRINT_NULL:
277 		break;
278 	case TEP_PRINT_ATOM:
279 		define_value(TEP_PRINT_FLAGS, ev_name, cur_field_name, "0",
280 			     args->atom.atom);
281 		zero_flag_atom = 0;
282 		break;
283 	case TEP_PRINT_FIELD:
284 		free(cur_field_name);
285 		cur_field_name = strdup(args->field.name);
286 		break;
287 	case TEP_PRINT_FLAGS:
288 		define_event_symbols(event, ev_name, args->flags.field);
289 		define_field(TEP_PRINT_FLAGS, ev_name, cur_field_name,
290 			     args->flags.delim);
291 		define_values(TEP_PRINT_FLAGS, args->flags.flags, ev_name,
292 			      cur_field_name);
293 		break;
294 	case TEP_PRINT_SYMBOL:
295 		define_event_symbols(event, ev_name, args->symbol.field);
296 		define_field(TEP_PRINT_SYMBOL, ev_name, cur_field_name, NULL);
297 		define_values(TEP_PRINT_SYMBOL, args->symbol.symbols, ev_name,
298 			      cur_field_name);
299 		break;
300 	case TEP_PRINT_HEX:
301 	case TEP_PRINT_HEX_STR:
302 		define_event_symbols(event, ev_name, args->hex.field);
303 		define_event_symbols(event, ev_name, args->hex.size);
304 		break;
305 	case TEP_PRINT_INT_ARRAY:
306 		define_event_symbols(event, ev_name, args->int_array.field);
307 		define_event_symbols(event, ev_name, args->int_array.count);
308 		define_event_symbols(event, ev_name, args->int_array.el_size);
309 		break;
310 	case TEP_PRINT_STRING:
311 		break;
312 	case TEP_PRINT_TYPE:
313 		define_event_symbols(event, ev_name, args->typecast.item);
314 		break;
315 	case TEP_PRINT_OP:
316 		if (strcmp(args->op.op, ":") == 0)
317 			zero_flag_atom = 1;
318 		define_event_symbols(event, ev_name, args->op.left);
319 		define_event_symbols(event, ev_name, args->op.right);
320 		break;
321 	default:
322 		/* gcc warns for these? */
323 	case TEP_PRINT_BSTRING:
324 	case TEP_PRINT_DYNAMIC_ARRAY:
325 	case TEP_PRINT_DYNAMIC_ARRAY_LEN:
326 	case TEP_PRINT_FUNC:
327 	case TEP_PRINT_BITMASK:
328 		/* we should warn... */
329 		return;
330 	}
331 
332 	if (args->next)
333 		define_event_symbols(event, ev_name, args->next);
334 }
335 
336 static PyObject *get_field_numeric_entry(struct tep_event *event,
337 		struct tep_format_field *field, void *data)
338 {
339 	bool is_array = field->flags & TEP_FIELD_IS_ARRAY;
340 	PyObject *obj = NULL, *list = NULL;
341 	unsigned long long val;
342 	unsigned int item_size, n_items, i;
343 
344 	if (is_array) {
345 		list = PyList_New(field->arraylen);
346 		item_size = field->size / field->arraylen;
347 		n_items = field->arraylen;
348 	} else {
349 		item_size = field->size;
350 		n_items = 1;
351 	}
352 
353 	for (i = 0; i < n_items; i++) {
354 
355 		val = read_size(event, data + field->offset + i * item_size,
356 				item_size);
357 		if (field->flags & TEP_FIELD_IS_SIGNED) {
358 			if ((long long)val >= LONG_MIN &&
359 					(long long)val <= LONG_MAX)
360 				obj = _PyLong_FromLong(val);
361 			else
362 				obj = PyLong_FromLongLong(val);
363 		} else {
364 			if (val <= LONG_MAX)
365 				obj = _PyLong_FromLong(val);
366 			else
367 				obj = PyLong_FromUnsignedLongLong(val);
368 		}
369 		if (is_array)
370 			PyList_SET_ITEM(list, i, obj);
371 	}
372 	if (is_array)
373 		obj = list;
374 	return obj;
375 }
376 
377 static const char *get_dsoname(struct map *map)
378 {
379 	const char *dsoname = "[unknown]";
380 
381 	if (map && map->dso) {
382 		if (symbol_conf.show_kernel_path && map->dso->long_name)
383 			dsoname = map->dso->long_name;
384 		else
385 			dsoname = map->dso->name;
386 	}
387 
388 	return dsoname;
389 }
390 
391 static PyObject *python_process_callchain(struct perf_sample *sample,
392 					 struct perf_evsel *evsel,
393 					 struct addr_location *al)
394 {
395 	PyObject *pylist;
396 
397 	pylist = PyList_New(0);
398 	if (!pylist)
399 		Py_FatalError("couldn't create Python list");
400 
401 	if (!symbol_conf.use_callchain || !sample->callchain)
402 		goto exit;
403 
404 	if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
405 				      sample, NULL, NULL,
406 				      scripting_max_stack) != 0) {
407 		pr_err("Failed to resolve callchain. Skipping\n");
408 		goto exit;
409 	}
410 	callchain_cursor_commit(&callchain_cursor);
411 
412 
413 	while (1) {
414 		PyObject *pyelem;
415 		struct callchain_cursor_node *node;
416 		node = callchain_cursor_current(&callchain_cursor);
417 		if (!node)
418 			break;
419 
420 		pyelem = PyDict_New();
421 		if (!pyelem)
422 			Py_FatalError("couldn't create Python dictionary");
423 
424 
425 		pydict_set_item_string_decref(pyelem, "ip",
426 				PyLong_FromUnsignedLongLong(node->ip));
427 
428 		if (node->sym) {
429 			PyObject *pysym  = PyDict_New();
430 			if (!pysym)
431 				Py_FatalError("couldn't create Python dictionary");
432 			pydict_set_item_string_decref(pysym, "start",
433 					PyLong_FromUnsignedLongLong(node->sym->start));
434 			pydict_set_item_string_decref(pysym, "end",
435 					PyLong_FromUnsignedLongLong(node->sym->end));
436 			pydict_set_item_string_decref(pysym, "binding",
437 					_PyLong_FromLong(node->sym->binding));
438 			pydict_set_item_string_decref(pysym, "name",
439 					_PyUnicode_FromStringAndSize(node->sym->name,
440 							node->sym->namelen));
441 			pydict_set_item_string_decref(pyelem, "sym", pysym);
442 		}
443 
444 		if (node->map) {
445 			const char *dsoname = get_dsoname(node->map);
446 
447 			pydict_set_item_string_decref(pyelem, "dso",
448 					_PyUnicode_FromString(dsoname));
449 		}
450 
451 		callchain_cursor_advance(&callchain_cursor);
452 		PyList_Append(pylist, pyelem);
453 		Py_DECREF(pyelem);
454 	}
455 
456 exit:
457 	return pylist;
458 }
459 
460 static PyObject *python_process_brstack(struct perf_sample *sample,
461 					struct thread *thread)
462 {
463 	struct branch_stack *br = sample->branch_stack;
464 	PyObject *pylist;
465 	u64 i;
466 
467 	pylist = PyList_New(0);
468 	if (!pylist)
469 		Py_FatalError("couldn't create Python list");
470 
471 	if (!(br && br->nr))
472 		goto exit;
473 
474 	for (i = 0; i < br->nr; i++) {
475 		PyObject *pyelem;
476 		struct addr_location al;
477 		const char *dsoname;
478 
479 		pyelem = PyDict_New();
480 		if (!pyelem)
481 			Py_FatalError("couldn't create Python dictionary");
482 
483 		pydict_set_item_string_decref(pyelem, "from",
484 		    PyLong_FromUnsignedLongLong(br->entries[i].from));
485 		pydict_set_item_string_decref(pyelem, "to",
486 		    PyLong_FromUnsignedLongLong(br->entries[i].to));
487 		pydict_set_item_string_decref(pyelem, "mispred",
488 		    PyBool_FromLong(br->entries[i].flags.mispred));
489 		pydict_set_item_string_decref(pyelem, "predicted",
490 		    PyBool_FromLong(br->entries[i].flags.predicted));
491 		pydict_set_item_string_decref(pyelem, "in_tx",
492 		    PyBool_FromLong(br->entries[i].flags.in_tx));
493 		pydict_set_item_string_decref(pyelem, "abort",
494 		    PyBool_FromLong(br->entries[i].flags.abort));
495 		pydict_set_item_string_decref(pyelem, "cycles",
496 		    PyLong_FromUnsignedLongLong(br->entries[i].flags.cycles));
497 
498 		thread__find_map_fb(thread, sample->cpumode,
499 				    br->entries[i].from, &al);
500 		dsoname = get_dsoname(al.map);
501 		pydict_set_item_string_decref(pyelem, "from_dsoname",
502 					      _PyUnicode_FromString(dsoname));
503 
504 		thread__find_map_fb(thread, sample->cpumode,
505 				    br->entries[i].to, &al);
506 		dsoname = get_dsoname(al.map);
507 		pydict_set_item_string_decref(pyelem, "to_dsoname",
508 					      _PyUnicode_FromString(dsoname));
509 
510 		PyList_Append(pylist, pyelem);
511 		Py_DECREF(pyelem);
512 	}
513 
514 exit:
515 	return pylist;
516 }
517 
518 static unsigned long get_offset(struct symbol *sym, struct addr_location *al)
519 {
520 	unsigned long offset;
521 
522 	if (al->addr < sym->end)
523 		offset = al->addr - sym->start;
524 	else
525 		offset = al->addr - al->map->start - sym->start;
526 
527 	return offset;
528 }
529 
530 static int get_symoff(struct symbol *sym, struct addr_location *al,
531 		      bool print_off, char *bf, int size)
532 {
533 	unsigned long offset;
534 
535 	if (!sym || !sym->name[0])
536 		return scnprintf(bf, size, "%s", "[unknown]");
537 
538 	if (!print_off)
539 		return scnprintf(bf, size, "%s", sym->name);
540 
541 	offset = get_offset(sym, al);
542 
543 	return scnprintf(bf, size, "%s+0x%x", sym->name, offset);
544 }
545 
546 static int get_br_mspred(struct branch_flags *flags, char *bf, int size)
547 {
548 	if (!flags->mispred  && !flags->predicted)
549 		return scnprintf(bf, size, "%s", "-");
550 
551 	if (flags->mispred)
552 		return scnprintf(bf, size, "%s", "M");
553 
554 	return scnprintf(bf, size, "%s", "P");
555 }
556 
557 static PyObject *python_process_brstacksym(struct perf_sample *sample,
558 					   struct thread *thread)
559 {
560 	struct branch_stack *br = sample->branch_stack;
561 	PyObject *pylist;
562 	u64 i;
563 	char bf[512];
564 	struct addr_location al;
565 
566 	pylist = PyList_New(0);
567 	if (!pylist)
568 		Py_FatalError("couldn't create Python list");
569 
570 	if (!(br && br->nr))
571 		goto exit;
572 
573 	for (i = 0; i < br->nr; i++) {
574 		PyObject *pyelem;
575 
576 		pyelem = PyDict_New();
577 		if (!pyelem)
578 			Py_FatalError("couldn't create Python dictionary");
579 
580 		thread__find_symbol_fb(thread, sample->cpumode,
581 				       br->entries[i].from, &al);
582 		get_symoff(al.sym, &al, true, bf, sizeof(bf));
583 		pydict_set_item_string_decref(pyelem, "from",
584 					      _PyUnicode_FromString(bf));
585 
586 		thread__find_symbol_fb(thread, sample->cpumode,
587 				       br->entries[i].to, &al);
588 		get_symoff(al.sym, &al, true, bf, sizeof(bf));
589 		pydict_set_item_string_decref(pyelem, "to",
590 					      _PyUnicode_FromString(bf));
591 
592 		get_br_mspred(&br->entries[i].flags, bf, sizeof(bf));
593 		pydict_set_item_string_decref(pyelem, "pred",
594 					      _PyUnicode_FromString(bf));
595 
596 		if (br->entries[i].flags.in_tx) {
597 			pydict_set_item_string_decref(pyelem, "in_tx",
598 					      _PyUnicode_FromString("X"));
599 		} else {
600 			pydict_set_item_string_decref(pyelem, "in_tx",
601 					      _PyUnicode_FromString("-"));
602 		}
603 
604 		if (br->entries[i].flags.abort) {
605 			pydict_set_item_string_decref(pyelem, "abort",
606 					      _PyUnicode_FromString("A"));
607 		} else {
608 			pydict_set_item_string_decref(pyelem, "abort",
609 					      _PyUnicode_FromString("-"));
610 		}
611 
612 		PyList_Append(pylist, pyelem);
613 		Py_DECREF(pyelem);
614 	}
615 
616 exit:
617 	return pylist;
618 }
619 
620 static PyObject *get_sample_value_as_tuple(struct sample_read_value *value)
621 {
622 	PyObject *t;
623 
624 	t = PyTuple_New(2);
625 	if (!t)
626 		Py_FatalError("couldn't create Python tuple");
627 	PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id));
628 	PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value));
629 	return t;
630 }
631 
632 static void set_sample_read_in_dict(PyObject *dict_sample,
633 					 struct perf_sample *sample,
634 					 struct perf_evsel *evsel)
635 {
636 	u64 read_format = evsel->attr.read_format;
637 	PyObject *values;
638 	unsigned int i;
639 
640 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
641 		pydict_set_item_string_decref(dict_sample, "time_enabled",
642 			PyLong_FromUnsignedLongLong(sample->read.time_enabled));
643 	}
644 
645 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
646 		pydict_set_item_string_decref(dict_sample, "time_running",
647 			PyLong_FromUnsignedLongLong(sample->read.time_running));
648 	}
649 
650 	if (read_format & PERF_FORMAT_GROUP)
651 		values = PyList_New(sample->read.group.nr);
652 	else
653 		values = PyList_New(1);
654 
655 	if (!values)
656 		Py_FatalError("couldn't create Python list");
657 
658 	if (read_format & PERF_FORMAT_GROUP) {
659 		for (i = 0; i < sample->read.group.nr; i++) {
660 			PyObject *t = get_sample_value_as_tuple(&sample->read.group.values[i]);
661 			PyList_SET_ITEM(values, i, t);
662 		}
663 	} else {
664 		PyObject *t = get_sample_value_as_tuple(&sample->read.one);
665 		PyList_SET_ITEM(values, 0, t);
666 	}
667 	pydict_set_item_string_decref(dict_sample, "values", values);
668 }
669 
670 static void set_sample_datasrc_in_dict(PyObject *dict,
671 				       struct perf_sample *sample)
672 {
673 	struct mem_info mi = { .data_src.val = sample->data_src };
674 	char decode[100];
675 
676 	pydict_set_item_string_decref(dict, "datasrc",
677 			PyLong_FromUnsignedLongLong(sample->data_src));
678 
679 	perf_script__meminfo_scnprintf(decode, 100, &mi);
680 
681 	pydict_set_item_string_decref(dict, "datasrc_decode",
682 			_PyUnicode_FromString(decode));
683 }
684 
685 static int regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size)
686 {
687 	unsigned int i = 0, r;
688 	int printed = 0;
689 
690 	bf[0] = 0;
691 
692 	for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
693 		u64 val = regs->regs[i++];
694 
695 		printed += scnprintf(bf + printed, size - printed,
696 				     "%5s:0x%" PRIx64 " ",
697 				     perf_reg_name(r), val);
698 	}
699 
700 	return printed;
701 }
702 
703 static void set_regs_in_dict(PyObject *dict,
704 			     struct perf_sample *sample,
705 			     struct perf_evsel *evsel)
706 {
707 	struct perf_event_attr *attr = &evsel->attr;
708 	char bf[512];
709 
710 	regs_map(&sample->intr_regs, attr->sample_regs_intr, bf, sizeof(bf));
711 
712 	pydict_set_item_string_decref(dict, "iregs",
713 			_PyUnicode_FromString(bf));
714 
715 	regs_map(&sample->user_regs, attr->sample_regs_user, bf, sizeof(bf));
716 
717 	pydict_set_item_string_decref(dict, "uregs",
718 			_PyUnicode_FromString(bf));
719 }
720 
721 static PyObject *get_perf_sample_dict(struct perf_sample *sample,
722 					 struct perf_evsel *evsel,
723 					 struct addr_location *al,
724 					 PyObject *callchain)
725 {
726 	PyObject *dict, *dict_sample, *brstack, *brstacksym;
727 
728 	dict = PyDict_New();
729 	if (!dict)
730 		Py_FatalError("couldn't create Python dictionary");
731 
732 	dict_sample = PyDict_New();
733 	if (!dict_sample)
734 		Py_FatalError("couldn't create Python dictionary");
735 
736 	pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel)));
737 	pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->attr, sizeof(evsel->attr)));
738 
739 	pydict_set_item_string_decref(dict_sample, "pid",
740 			_PyLong_FromLong(sample->pid));
741 	pydict_set_item_string_decref(dict_sample, "tid",
742 			_PyLong_FromLong(sample->tid));
743 	pydict_set_item_string_decref(dict_sample, "cpu",
744 			_PyLong_FromLong(sample->cpu));
745 	pydict_set_item_string_decref(dict_sample, "ip",
746 			PyLong_FromUnsignedLongLong(sample->ip));
747 	pydict_set_item_string_decref(dict_sample, "time",
748 			PyLong_FromUnsignedLongLong(sample->time));
749 	pydict_set_item_string_decref(dict_sample, "period",
750 			PyLong_FromUnsignedLongLong(sample->period));
751 	pydict_set_item_string_decref(dict_sample, "phys_addr",
752 			PyLong_FromUnsignedLongLong(sample->phys_addr));
753 	pydict_set_item_string_decref(dict_sample, "addr",
754 			PyLong_FromUnsignedLongLong(sample->addr));
755 	set_sample_read_in_dict(dict_sample, sample, evsel);
756 	pydict_set_item_string_decref(dict_sample, "weight",
757 			PyLong_FromUnsignedLongLong(sample->weight));
758 	pydict_set_item_string_decref(dict_sample, "transaction",
759 			PyLong_FromUnsignedLongLong(sample->transaction));
760 	set_sample_datasrc_in_dict(dict_sample, sample);
761 	pydict_set_item_string_decref(dict, "sample", dict_sample);
762 
763 	pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize(
764 			(const char *)sample->raw_data, sample->raw_size));
765 	pydict_set_item_string_decref(dict, "comm",
766 			_PyUnicode_FromString(thread__comm_str(al->thread)));
767 	if (al->map) {
768 		pydict_set_item_string_decref(dict, "dso",
769 			_PyUnicode_FromString(al->map->dso->name));
770 	}
771 	if (al->sym) {
772 		pydict_set_item_string_decref(dict, "symbol",
773 			_PyUnicode_FromString(al->sym->name));
774 	}
775 
776 	pydict_set_item_string_decref(dict, "callchain", callchain);
777 
778 	brstack = python_process_brstack(sample, al->thread);
779 	pydict_set_item_string_decref(dict, "brstack", brstack);
780 
781 	brstacksym = python_process_brstacksym(sample, al->thread);
782 	pydict_set_item_string_decref(dict, "brstacksym", brstacksym);
783 
784 	set_regs_in_dict(dict, sample, evsel);
785 
786 	return dict;
787 }
788 
789 static void python_process_tracepoint(struct perf_sample *sample,
790 				      struct perf_evsel *evsel,
791 				      struct addr_location *al)
792 {
793 	struct tep_event *event = evsel->tp_format;
794 	PyObject *handler, *context, *t, *obj = NULL, *callchain;
795 	PyObject *dict = NULL, *all_entries_dict = NULL;
796 	static char handler_name[256];
797 	struct tep_format_field *field;
798 	unsigned long s, ns;
799 	unsigned n = 0;
800 	int pid;
801 	int cpu = sample->cpu;
802 	void *data = sample->raw_data;
803 	unsigned long long nsecs = sample->time;
804 	const char *comm = thread__comm_str(al->thread);
805 	const char *default_handler_name = "trace_unhandled";
806 
807 	if (!event) {
808 		snprintf(handler_name, sizeof(handler_name),
809 			 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
810 		Py_FatalError(handler_name);
811 	}
812 
813 	pid = raw_field_value(event, "common_pid", data);
814 
815 	sprintf(handler_name, "%s__%s", event->system, event->name);
816 
817 	if (!test_and_set_bit(event->id, events_defined))
818 		define_event_symbols(event, handler_name, event->print_fmt.args);
819 
820 	handler = get_handler(handler_name);
821 	if (!handler) {
822 		handler = get_handler(default_handler_name);
823 		if (!handler)
824 			return;
825 		dict = PyDict_New();
826 		if (!dict)
827 			Py_FatalError("couldn't create Python dict");
828 	}
829 
830 	t = PyTuple_New(MAX_FIELDS);
831 	if (!t)
832 		Py_FatalError("couldn't create Python tuple");
833 
834 
835 	s = nsecs / NSEC_PER_SEC;
836 	ns = nsecs - s * NSEC_PER_SEC;
837 
838 	scripting_context->event_data = data;
839 	scripting_context->pevent = evsel->tp_format->pevent;
840 
841 	context = _PyCapsule_New(scripting_context, NULL, NULL);
842 
843 	PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name));
844 	PyTuple_SetItem(t, n++, context);
845 
846 	/* ip unwinding */
847 	callchain = python_process_callchain(sample, evsel, al);
848 	/* Need an additional reference for the perf_sample dict */
849 	Py_INCREF(callchain);
850 
851 	if (!dict) {
852 		PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
853 		PyTuple_SetItem(t, n++, _PyLong_FromLong(s));
854 		PyTuple_SetItem(t, n++, _PyLong_FromLong(ns));
855 		PyTuple_SetItem(t, n++, _PyLong_FromLong(pid));
856 		PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm));
857 		PyTuple_SetItem(t, n++, callchain);
858 	} else {
859 		pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu));
860 		pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s));
861 		pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns));
862 		pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid));
863 		pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm));
864 		pydict_set_item_string_decref(dict, "common_callchain", callchain);
865 	}
866 	for (field = event->format.fields; field; field = field->next) {
867 		unsigned int offset, len;
868 		unsigned long long val;
869 
870 		if (field->flags & TEP_FIELD_IS_ARRAY) {
871 			offset = field->offset;
872 			len    = field->size;
873 			if (field->flags & TEP_FIELD_IS_DYNAMIC) {
874 				val     = tep_read_number(scripting_context->pevent,
875 							  data + offset, len);
876 				offset  = val;
877 				len     = offset >> 16;
878 				offset &= 0xffff;
879 			}
880 			if (field->flags & TEP_FIELD_IS_STRING &&
881 			    is_printable_array(data + offset, len)) {
882 				obj = _PyUnicode_FromString((char *) data + offset);
883 			} else {
884 				obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
885 				field->flags &= ~TEP_FIELD_IS_STRING;
886 			}
887 		} else { /* FIELD_IS_NUMERIC */
888 			obj = get_field_numeric_entry(event, field, data);
889 		}
890 		if (!dict)
891 			PyTuple_SetItem(t, n++, obj);
892 		else
893 			pydict_set_item_string_decref(dict, field->name, obj);
894 
895 	}
896 
897 	if (dict)
898 		PyTuple_SetItem(t, n++, dict);
899 
900 	if (get_argument_count(handler) == (int) n + 1) {
901 		all_entries_dict = get_perf_sample_dict(sample, evsel, al,
902 			callchain);
903 		PyTuple_SetItem(t, n++,	all_entries_dict);
904 	} else {
905 		Py_DECREF(callchain);
906 	}
907 
908 	if (_PyTuple_Resize(&t, n) == -1)
909 		Py_FatalError("error resizing Python tuple");
910 
911 	if (!dict)
912 		call_object(handler, t, handler_name);
913 	else
914 		call_object(handler, t, default_handler_name);
915 
916 	Py_DECREF(t);
917 }
918 
919 static PyObject *tuple_new(unsigned int sz)
920 {
921 	PyObject *t;
922 
923 	t = PyTuple_New(sz);
924 	if (!t)
925 		Py_FatalError("couldn't create Python tuple");
926 	return t;
927 }
928 
929 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
930 {
931 #if BITS_PER_LONG == 64
932 	return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
933 #endif
934 #if BITS_PER_LONG == 32
935 	return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
936 #endif
937 }
938 
939 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
940 {
941 	return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
942 }
943 
944 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
945 {
946 	return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s));
947 }
948 
949 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
950 {
951 	struct tables *tables = container_of(dbe, struct tables, dbe);
952 	PyObject *t;
953 
954 	t = tuple_new(2);
955 
956 	tuple_set_u64(t, 0, evsel->db_id);
957 	tuple_set_string(t, 1, perf_evsel__name(evsel));
958 
959 	call_object(tables->evsel_handler, t, "evsel_table");
960 
961 	Py_DECREF(t);
962 
963 	return 0;
964 }
965 
966 static int python_export_machine(struct db_export *dbe,
967 				 struct machine *machine)
968 {
969 	struct tables *tables = container_of(dbe, struct tables, dbe);
970 	PyObject *t;
971 
972 	t = tuple_new(3);
973 
974 	tuple_set_u64(t, 0, machine->db_id);
975 	tuple_set_s32(t, 1, machine->pid);
976 	tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
977 
978 	call_object(tables->machine_handler, t, "machine_table");
979 
980 	Py_DECREF(t);
981 
982 	return 0;
983 }
984 
985 static int python_export_thread(struct db_export *dbe, struct thread *thread,
986 				u64 main_thread_db_id, struct machine *machine)
987 {
988 	struct tables *tables = container_of(dbe, struct tables, dbe);
989 	PyObject *t;
990 
991 	t = tuple_new(5);
992 
993 	tuple_set_u64(t, 0, thread->db_id);
994 	tuple_set_u64(t, 1, machine->db_id);
995 	tuple_set_u64(t, 2, main_thread_db_id);
996 	tuple_set_s32(t, 3, thread->pid_);
997 	tuple_set_s32(t, 4, thread->tid);
998 
999 	call_object(tables->thread_handler, t, "thread_table");
1000 
1001 	Py_DECREF(t);
1002 
1003 	return 0;
1004 }
1005 
1006 static int python_export_comm(struct db_export *dbe, struct comm *comm)
1007 {
1008 	struct tables *tables = container_of(dbe, struct tables, dbe);
1009 	PyObject *t;
1010 
1011 	t = tuple_new(2);
1012 
1013 	tuple_set_u64(t, 0, comm->db_id);
1014 	tuple_set_string(t, 1, comm__str(comm));
1015 
1016 	call_object(tables->comm_handler, t, "comm_table");
1017 
1018 	Py_DECREF(t);
1019 
1020 	return 0;
1021 }
1022 
1023 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
1024 				     struct comm *comm, struct thread *thread)
1025 {
1026 	struct tables *tables = container_of(dbe, struct tables, dbe);
1027 	PyObject *t;
1028 
1029 	t = tuple_new(3);
1030 
1031 	tuple_set_u64(t, 0, db_id);
1032 	tuple_set_u64(t, 1, comm->db_id);
1033 	tuple_set_u64(t, 2, thread->db_id);
1034 
1035 	call_object(tables->comm_thread_handler, t, "comm_thread_table");
1036 
1037 	Py_DECREF(t);
1038 
1039 	return 0;
1040 }
1041 
1042 static int python_export_dso(struct db_export *dbe, struct dso *dso,
1043 			     struct machine *machine)
1044 {
1045 	struct tables *tables = container_of(dbe, struct tables, dbe);
1046 	char sbuild_id[SBUILD_ID_SIZE];
1047 	PyObject *t;
1048 
1049 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1050 
1051 	t = tuple_new(5);
1052 
1053 	tuple_set_u64(t, 0, dso->db_id);
1054 	tuple_set_u64(t, 1, machine->db_id);
1055 	tuple_set_string(t, 2, dso->short_name);
1056 	tuple_set_string(t, 3, dso->long_name);
1057 	tuple_set_string(t, 4, sbuild_id);
1058 
1059 	call_object(tables->dso_handler, t, "dso_table");
1060 
1061 	Py_DECREF(t);
1062 
1063 	return 0;
1064 }
1065 
1066 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
1067 				struct dso *dso)
1068 {
1069 	struct tables *tables = container_of(dbe, struct tables, dbe);
1070 	u64 *sym_db_id = symbol__priv(sym);
1071 	PyObject *t;
1072 
1073 	t = tuple_new(6);
1074 
1075 	tuple_set_u64(t, 0, *sym_db_id);
1076 	tuple_set_u64(t, 1, dso->db_id);
1077 	tuple_set_u64(t, 2, sym->start);
1078 	tuple_set_u64(t, 3, sym->end);
1079 	tuple_set_s32(t, 4, sym->binding);
1080 	tuple_set_string(t, 5, sym->name);
1081 
1082 	call_object(tables->symbol_handler, t, "symbol_table");
1083 
1084 	Py_DECREF(t);
1085 
1086 	return 0;
1087 }
1088 
1089 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
1090 				     const char *name)
1091 {
1092 	struct tables *tables = container_of(dbe, struct tables, dbe);
1093 	PyObject *t;
1094 
1095 	t = tuple_new(2);
1096 
1097 	tuple_set_s32(t, 0, branch_type);
1098 	tuple_set_string(t, 1, name);
1099 
1100 	call_object(tables->branch_type_handler, t, "branch_type_table");
1101 
1102 	Py_DECREF(t);
1103 
1104 	return 0;
1105 }
1106 
1107 static int python_export_sample(struct db_export *dbe,
1108 				struct export_sample *es)
1109 {
1110 	struct tables *tables = container_of(dbe, struct tables, dbe);
1111 	PyObject *t;
1112 
1113 	t = tuple_new(22);
1114 
1115 	tuple_set_u64(t, 0, es->db_id);
1116 	tuple_set_u64(t, 1, es->evsel->db_id);
1117 	tuple_set_u64(t, 2, es->al->machine->db_id);
1118 	tuple_set_u64(t, 3, es->al->thread->db_id);
1119 	tuple_set_u64(t, 4, es->comm_db_id);
1120 	tuple_set_u64(t, 5, es->dso_db_id);
1121 	tuple_set_u64(t, 6, es->sym_db_id);
1122 	tuple_set_u64(t, 7, es->offset);
1123 	tuple_set_u64(t, 8, es->sample->ip);
1124 	tuple_set_u64(t, 9, es->sample->time);
1125 	tuple_set_s32(t, 10, es->sample->cpu);
1126 	tuple_set_u64(t, 11, es->addr_dso_db_id);
1127 	tuple_set_u64(t, 12, es->addr_sym_db_id);
1128 	tuple_set_u64(t, 13, es->addr_offset);
1129 	tuple_set_u64(t, 14, es->sample->addr);
1130 	tuple_set_u64(t, 15, es->sample->period);
1131 	tuple_set_u64(t, 16, es->sample->weight);
1132 	tuple_set_u64(t, 17, es->sample->transaction);
1133 	tuple_set_u64(t, 18, es->sample->data_src);
1134 	tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
1135 	tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
1136 	tuple_set_u64(t, 21, es->call_path_id);
1137 
1138 	call_object(tables->sample_handler, t, "sample_table");
1139 
1140 	Py_DECREF(t);
1141 
1142 	return 0;
1143 }
1144 
1145 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
1146 {
1147 	struct tables *tables = container_of(dbe, struct tables, dbe);
1148 	PyObject *t;
1149 	u64 parent_db_id, sym_db_id;
1150 
1151 	parent_db_id = cp->parent ? cp->parent->db_id : 0;
1152 	sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
1153 
1154 	t = tuple_new(4);
1155 
1156 	tuple_set_u64(t, 0, cp->db_id);
1157 	tuple_set_u64(t, 1, parent_db_id);
1158 	tuple_set_u64(t, 2, sym_db_id);
1159 	tuple_set_u64(t, 3, cp->ip);
1160 
1161 	call_object(tables->call_path_handler, t, "call_path_table");
1162 
1163 	Py_DECREF(t);
1164 
1165 	return 0;
1166 }
1167 
1168 static int python_export_call_return(struct db_export *dbe,
1169 				     struct call_return *cr)
1170 {
1171 	struct tables *tables = container_of(dbe, struct tables, dbe);
1172 	u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
1173 	PyObject *t;
1174 
1175 	t = tuple_new(11);
1176 
1177 	tuple_set_u64(t, 0, cr->db_id);
1178 	tuple_set_u64(t, 1, cr->thread->db_id);
1179 	tuple_set_u64(t, 2, comm_db_id);
1180 	tuple_set_u64(t, 3, cr->cp->db_id);
1181 	tuple_set_u64(t, 4, cr->call_time);
1182 	tuple_set_u64(t, 5, cr->return_time);
1183 	tuple_set_u64(t, 6, cr->branch_count);
1184 	tuple_set_u64(t, 7, cr->call_ref);
1185 	tuple_set_u64(t, 8, cr->return_ref);
1186 	tuple_set_u64(t, 9, cr->cp->parent->db_id);
1187 	tuple_set_s32(t, 10, cr->flags);
1188 
1189 	call_object(tables->call_return_handler, t, "call_return_table");
1190 
1191 	Py_DECREF(t);
1192 
1193 	return 0;
1194 }
1195 
1196 static int python_process_call_return(struct call_return *cr, void *data)
1197 {
1198 	struct db_export *dbe = data;
1199 
1200 	return db_export__call_return(dbe, cr);
1201 }
1202 
1203 static void python_process_general_event(struct perf_sample *sample,
1204 					 struct perf_evsel *evsel,
1205 					 struct addr_location *al)
1206 {
1207 	PyObject *handler, *t, *dict, *callchain;
1208 	static char handler_name[64];
1209 	unsigned n = 0;
1210 
1211 	snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
1212 
1213 	handler = get_handler(handler_name);
1214 	if (!handler)
1215 		return;
1216 
1217 	/*
1218 	 * Use the MAX_FIELDS to make the function expandable, though
1219 	 * currently there is only one item for the tuple.
1220 	 */
1221 	t = PyTuple_New(MAX_FIELDS);
1222 	if (!t)
1223 		Py_FatalError("couldn't create Python tuple");
1224 
1225 	/* ip unwinding */
1226 	callchain = python_process_callchain(sample, evsel, al);
1227 	dict = get_perf_sample_dict(sample, evsel, al, callchain);
1228 
1229 	PyTuple_SetItem(t, n++, dict);
1230 	if (_PyTuple_Resize(&t, n) == -1)
1231 		Py_FatalError("error resizing Python tuple");
1232 
1233 	call_object(handler, t, handler_name);
1234 
1235 	Py_DECREF(t);
1236 }
1237 
1238 static void python_process_event(union perf_event *event,
1239 				 struct perf_sample *sample,
1240 				 struct perf_evsel *evsel,
1241 				 struct addr_location *al)
1242 {
1243 	struct tables *tables = &tables_global;
1244 
1245 	switch (evsel->attr.type) {
1246 	case PERF_TYPE_TRACEPOINT:
1247 		python_process_tracepoint(sample, evsel, al);
1248 		break;
1249 	/* Reserve for future process_hw/sw/raw APIs */
1250 	default:
1251 		if (tables->db_export_mode)
1252 			db_export__sample(&tables->dbe, event, sample, evsel, al);
1253 		else
1254 			python_process_general_event(sample, evsel, al);
1255 	}
1256 }
1257 
1258 static void get_handler_name(char *str, size_t size,
1259 			     struct perf_evsel *evsel)
1260 {
1261 	char *p = str;
1262 
1263 	scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
1264 
1265 	while ((p = strchr(p, ':'))) {
1266 		*p = '_';
1267 		p++;
1268 	}
1269 }
1270 
1271 static void
1272 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
1273 	     struct perf_counts_values *count)
1274 {
1275 	PyObject *handler, *t;
1276 	static char handler_name[256];
1277 	int n = 0;
1278 
1279 	t = PyTuple_New(MAX_FIELDS);
1280 	if (!t)
1281 		Py_FatalError("couldn't create Python tuple");
1282 
1283 	get_handler_name(handler_name, sizeof(handler_name),
1284 			 counter);
1285 
1286 	handler = get_handler(handler_name);
1287 	if (!handler) {
1288 		pr_debug("can't find python handler %s\n", handler_name);
1289 		return;
1290 	}
1291 
1292 	PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
1293 	PyTuple_SetItem(t, n++, _PyLong_FromLong(thread));
1294 
1295 	tuple_set_u64(t, n++, tstamp);
1296 	tuple_set_u64(t, n++, count->val);
1297 	tuple_set_u64(t, n++, count->ena);
1298 	tuple_set_u64(t, n++, count->run);
1299 
1300 	if (_PyTuple_Resize(&t, n) == -1)
1301 		Py_FatalError("error resizing Python tuple");
1302 
1303 	call_object(handler, t, handler_name);
1304 
1305 	Py_DECREF(t);
1306 }
1307 
1308 static void python_process_stat(struct perf_stat_config *config,
1309 				struct perf_evsel *counter, u64 tstamp)
1310 {
1311 	struct thread_map *threads = counter->threads;
1312 	struct cpu_map *cpus = counter->cpus;
1313 	int cpu, thread;
1314 
1315 	if (config->aggr_mode == AGGR_GLOBAL) {
1316 		process_stat(counter, -1, -1, tstamp,
1317 			     &counter->counts->aggr);
1318 		return;
1319 	}
1320 
1321 	for (thread = 0; thread < threads->nr; thread++) {
1322 		for (cpu = 0; cpu < cpus->nr; cpu++) {
1323 			process_stat(counter, cpus->map[cpu],
1324 				     thread_map__pid(threads, thread), tstamp,
1325 				     perf_counts(counter->counts, cpu, thread));
1326 		}
1327 	}
1328 }
1329 
1330 static void python_process_stat_interval(u64 tstamp)
1331 {
1332 	PyObject *handler, *t;
1333 	static const char handler_name[] = "stat__interval";
1334 	int n = 0;
1335 
1336 	t = PyTuple_New(MAX_FIELDS);
1337 	if (!t)
1338 		Py_FatalError("couldn't create Python tuple");
1339 
1340 	handler = get_handler(handler_name);
1341 	if (!handler) {
1342 		pr_debug("can't find python handler %s\n", handler_name);
1343 		return;
1344 	}
1345 
1346 	tuple_set_u64(t, n++, tstamp);
1347 
1348 	if (_PyTuple_Resize(&t, n) == -1)
1349 		Py_FatalError("error resizing Python tuple");
1350 
1351 	call_object(handler, t, handler_name);
1352 
1353 	Py_DECREF(t);
1354 }
1355 
1356 static int run_start_sub(void)
1357 {
1358 	main_module = PyImport_AddModule("__main__");
1359 	if (main_module == NULL)
1360 		return -1;
1361 	Py_INCREF(main_module);
1362 
1363 	main_dict = PyModule_GetDict(main_module);
1364 	if (main_dict == NULL)
1365 		goto error;
1366 	Py_INCREF(main_dict);
1367 
1368 	try_call_object("trace_begin", NULL);
1369 
1370 	return 0;
1371 
1372 error:
1373 	Py_XDECREF(main_dict);
1374 	Py_XDECREF(main_module);
1375 	return -1;
1376 }
1377 
1378 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do {		\
1379 	tables->handler_name = get_handler(#table_name);		\
1380 	if (tables->handler_name)					\
1381 		tables->dbe.export_ ## name = python_export_ ## name;	\
1382 } while (0)
1383 
1384 #define SET_TABLE_HANDLER(name) \
1385 	SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1386 
1387 static void set_table_handlers(struct tables *tables)
1388 {
1389 	const char *perf_db_export_mode = "perf_db_export_mode";
1390 	const char *perf_db_export_calls = "perf_db_export_calls";
1391 	const char *perf_db_export_callchains = "perf_db_export_callchains";
1392 	PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1393 	bool export_calls = false;
1394 	bool export_callchains = false;
1395 	int ret;
1396 
1397 	memset(tables, 0, sizeof(struct tables));
1398 	if (db_export__init(&tables->dbe))
1399 		Py_FatalError("failed to initialize export");
1400 
1401 	db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1402 	if (!db_export_mode)
1403 		return;
1404 
1405 	ret = PyObject_IsTrue(db_export_mode);
1406 	if (ret == -1)
1407 		handler_call_die(perf_db_export_mode);
1408 	if (!ret)
1409 		return;
1410 
1411 	/* handle export calls */
1412 	tables->dbe.crp = NULL;
1413 	db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1414 	if (db_export_calls) {
1415 		ret = PyObject_IsTrue(db_export_calls);
1416 		if (ret == -1)
1417 			handler_call_die(perf_db_export_calls);
1418 		export_calls = !!ret;
1419 	}
1420 
1421 	if (export_calls) {
1422 		tables->dbe.crp =
1423 			call_return_processor__new(python_process_call_return,
1424 						   &tables->dbe);
1425 		if (!tables->dbe.crp)
1426 			Py_FatalError("failed to create calls processor");
1427 	}
1428 
1429 	/* handle export callchains */
1430 	tables->dbe.cpr = NULL;
1431 	db_export_callchains = PyDict_GetItemString(main_dict,
1432 						    perf_db_export_callchains);
1433 	if (db_export_callchains) {
1434 		ret = PyObject_IsTrue(db_export_callchains);
1435 		if (ret == -1)
1436 			handler_call_die(perf_db_export_callchains);
1437 		export_callchains = !!ret;
1438 	}
1439 
1440 	if (export_callchains) {
1441 		/*
1442 		 * Attempt to use the call path root from the call return
1443 		 * processor, if the call return processor is in use. Otherwise,
1444 		 * we allocate a new call path root. This prevents exporting
1445 		 * duplicate call path ids when both are in use simultaniously.
1446 		 */
1447 		if (tables->dbe.crp)
1448 			tables->dbe.cpr = tables->dbe.crp->cpr;
1449 		else
1450 			tables->dbe.cpr = call_path_root__new();
1451 
1452 		if (!tables->dbe.cpr)
1453 			Py_FatalError("failed to create call path root");
1454 	}
1455 
1456 	tables->db_export_mode = true;
1457 	/*
1458 	 * Reserve per symbol space for symbol->db_id via symbol__priv()
1459 	 */
1460 	symbol_conf.priv_size = sizeof(u64);
1461 
1462 	SET_TABLE_HANDLER(evsel);
1463 	SET_TABLE_HANDLER(machine);
1464 	SET_TABLE_HANDLER(thread);
1465 	SET_TABLE_HANDLER(comm);
1466 	SET_TABLE_HANDLER(comm_thread);
1467 	SET_TABLE_HANDLER(dso);
1468 	SET_TABLE_HANDLER(symbol);
1469 	SET_TABLE_HANDLER(branch_type);
1470 	SET_TABLE_HANDLER(sample);
1471 	SET_TABLE_HANDLER(call_path);
1472 	SET_TABLE_HANDLER(call_return);
1473 }
1474 
1475 #if PY_MAJOR_VERSION < 3
1476 static void _free_command_line(const char **command_line, int num)
1477 {
1478 	free(command_line);
1479 }
1480 #else
1481 static void _free_command_line(wchar_t **command_line, int num)
1482 {
1483 	int i;
1484 	for (i = 0; i < num; i++)
1485 		PyMem_RawFree(command_line[i]);
1486 	free(command_line);
1487 }
1488 #endif
1489 
1490 
1491 /*
1492  * Start trace script
1493  */
1494 static int python_start_script(const char *script, int argc, const char **argv)
1495 {
1496 	struct tables *tables = &tables_global;
1497 	PyMODINIT_FUNC (*initfunc)(void);
1498 #if PY_MAJOR_VERSION < 3
1499 	const char **command_line;
1500 #else
1501 	wchar_t **command_line;
1502 #endif
1503 	/*
1504 	 * Use a non-const name variable to cope with python 2.6's
1505 	 * PyImport_AppendInittab prototype
1506 	 */
1507 	char buf[PATH_MAX], name[19] = "perf_trace_context";
1508 	int i, err = 0;
1509 	FILE *fp;
1510 
1511 #if PY_MAJOR_VERSION < 3
1512 	initfunc = initperf_trace_context;
1513 	command_line = malloc((argc + 1) * sizeof(const char *));
1514 	command_line[0] = script;
1515 	for (i = 1; i < argc + 1; i++)
1516 		command_line[i] = argv[i - 1];
1517 #else
1518 	initfunc = PyInit_perf_trace_context;
1519 	command_line = malloc((argc + 1) * sizeof(wchar_t *));
1520 	command_line[0] = Py_DecodeLocale(script, NULL);
1521 	for (i = 1; i < argc + 1; i++)
1522 		command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
1523 #endif
1524 
1525 	PyImport_AppendInittab(name, initfunc);
1526 	Py_Initialize();
1527 
1528 #if PY_MAJOR_VERSION < 3
1529 	PySys_SetArgv(argc + 1, (char **)command_line);
1530 #else
1531 	PySys_SetArgv(argc + 1, command_line);
1532 #endif
1533 
1534 	fp = fopen(script, "r");
1535 	if (!fp) {
1536 		sprintf(buf, "Can't open python script \"%s\"", script);
1537 		perror(buf);
1538 		err = -1;
1539 		goto error;
1540 	}
1541 
1542 	err = PyRun_SimpleFile(fp, script);
1543 	if (err) {
1544 		fprintf(stderr, "Error running python script %s\n", script);
1545 		goto error;
1546 	}
1547 
1548 	err = run_start_sub();
1549 	if (err) {
1550 		fprintf(stderr, "Error starting python script %s\n", script);
1551 		goto error;
1552 	}
1553 
1554 	set_table_handlers(tables);
1555 
1556 	if (tables->db_export_mode) {
1557 		err = db_export__branch_types(&tables->dbe);
1558 		if (err)
1559 			goto error;
1560 	}
1561 
1562 	_free_command_line(command_line, argc + 1);
1563 
1564 	return err;
1565 error:
1566 	Py_Finalize();
1567 	_free_command_line(command_line, argc + 1);
1568 
1569 	return err;
1570 }
1571 
1572 static int python_flush_script(void)
1573 {
1574 	struct tables *tables = &tables_global;
1575 
1576 	return db_export__flush(&tables->dbe);
1577 }
1578 
1579 /*
1580  * Stop trace script
1581  */
1582 static int python_stop_script(void)
1583 {
1584 	struct tables *tables = &tables_global;
1585 
1586 	try_call_object("trace_end", NULL);
1587 
1588 	db_export__exit(&tables->dbe);
1589 
1590 	Py_XDECREF(main_dict);
1591 	Py_XDECREF(main_module);
1592 	Py_Finalize();
1593 
1594 	return 0;
1595 }
1596 
1597 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
1598 {
1599 	struct tep_event *event = NULL;
1600 	struct tep_format_field *f;
1601 	char fname[PATH_MAX];
1602 	int not_first, count;
1603 	FILE *ofp;
1604 
1605 	sprintf(fname, "%s.py", outfile);
1606 	ofp = fopen(fname, "w");
1607 	if (ofp == NULL) {
1608 		fprintf(stderr, "couldn't open %s\n", fname);
1609 		return -1;
1610 	}
1611 	fprintf(ofp, "# perf script event handlers, "
1612 		"generated by perf script -g python\n");
1613 
1614 	fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1615 		" License version 2\n\n");
1616 
1617 	fprintf(ofp, "# The common_* event handler fields are the most useful "
1618 		"fields common to\n");
1619 
1620 	fprintf(ofp, "# all events.  They don't necessarily correspond to "
1621 		"the 'common_*' fields\n");
1622 
1623 	fprintf(ofp, "# in the format files.  Those fields not available as "
1624 		"handler params can\n");
1625 
1626 	fprintf(ofp, "# be retrieved using Python functions of the form "
1627 		"common_*(context).\n");
1628 
1629 	fprintf(ofp, "# See the perf-script-python Documentation for the list "
1630 		"of available functions.\n\n");
1631 
1632 	fprintf(ofp, "from __future__ import print_function\n\n");
1633 	fprintf(ofp, "import os\n");
1634 	fprintf(ofp, "import sys\n\n");
1635 
1636 	fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1637 	fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1638 	fprintf(ofp, "\nfrom perf_trace_context import *\n");
1639 	fprintf(ofp, "from Core import *\n\n\n");
1640 
1641 	fprintf(ofp, "def trace_begin():\n");
1642 	fprintf(ofp, "\tprint(\"in trace_begin\")\n\n");
1643 
1644 	fprintf(ofp, "def trace_end():\n");
1645 	fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
1646 
1647 	while ((event = trace_find_next_event(pevent, event))) {
1648 		fprintf(ofp, "def %s__%s(", event->system, event->name);
1649 		fprintf(ofp, "event_name, ");
1650 		fprintf(ofp, "context, ");
1651 		fprintf(ofp, "common_cpu,\n");
1652 		fprintf(ofp, "\tcommon_secs, ");
1653 		fprintf(ofp, "common_nsecs, ");
1654 		fprintf(ofp, "common_pid, ");
1655 		fprintf(ofp, "common_comm,\n\t");
1656 		fprintf(ofp, "common_callchain, ");
1657 
1658 		not_first = 0;
1659 		count = 0;
1660 
1661 		for (f = event->format.fields; f; f = f->next) {
1662 			if (not_first++)
1663 				fprintf(ofp, ", ");
1664 			if (++count % 5 == 0)
1665 				fprintf(ofp, "\n\t");
1666 
1667 			fprintf(ofp, "%s", f->name);
1668 		}
1669 		if (not_first++)
1670 			fprintf(ofp, ", ");
1671 		if (++count % 5 == 0)
1672 			fprintf(ofp, "\n\t\t");
1673 		fprintf(ofp, "perf_sample_dict");
1674 
1675 		fprintf(ofp, "):\n");
1676 
1677 		fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1678 			"common_secs, common_nsecs,\n\t\t\t"
1679 			"common_pid, common_comm)\n\n");
1680 
1681 		fprintf(ofp, "\t\tprint(\"");
1682 
1683 		not_first = 0;
1684 		count = 0;
1685 
1686 		for (f = event->format.fields; f; f = f->next) {
1687 			if (not_first++)
1688 				fprintf(ofp, ", ");
1689 			if (count && count % 3 == 0) {
1690 				fprintf(ofp, "\" \\\n\t\t\"");
1691 			}
1692 			count++;
1693 
1694 			fprintf(ofp, "%s=", f->name);
1695 			if (f->flags & TEP_FIELD_IS_STRING ||
1696 			    f->flags & TEP_FIELD_IS_FLAG ||
1697 			    f->flags & TEP_FIELD_IS_ARRAY ||
1698 			    f->flags & TEP_FIELD_IS_SYMBOLIC)
1699 				fprintf(ofp, "%%s");
1700 			else if (f->flags & TEP_FIELD_IS_SIGNED)
1701 				fprintf(ofp, "%%d");
1702 			else
1703 				fprintf(ofp, "%%u");
1704 		}
1705 
1706 		fprintf(ofp, "\" %% \\\n\t\t(");
1707 
1708 		not_first = 0;
1709 		count = 0;
1710 
1711 		for (f = event->format.fields; f; f = f->next) {
1712 			if (not_first++)
1713 				fprintf(ofp, ", ");
1714 
1715 			if (++count % 5 == 0)
1716 				fprintf(ofp, "\n\t\t");
1717 
1718 			if (f->flags & TEP_FIELD_IS_FLAG) {
1719 				if ((count - 1) % 5 != 0) {
1720 					fprintf(ofp, "\n\t\t");
1721 					count = 4;
1722 				}
1723 				fprintf(ofp, "flag_str(\"");
1724 				fprintf(ofp, "%s__%s\", ", event->system,
1725 					event->name);
1726 				fprintf(ofp, "\"%s\", %s)", f->name,
1727 					f->name);
1728 			} else if (f->flags & TEP_FIELD_IS_SYMBOLIC) {
1729 				if ((count - 1) % 5 != 0) {
1730 					fprintf(ofp, "\n\t\t");
1731 					count = 4;
1732 				}
1733 				fprintf(ofp, "symbol_str(\"");
1734 				fprintf(ofp, "%s__%s\", ", event->system,
1735 					event->name);
1736 				fprintf(ofp, "\"%s\", %s)", f->name,
1737 					f->name);
1738 			} else
1739 				fprintf(ofp, "%s", f->name);
1740 		}
1741 
1742 		fprintf(ofp, "))\n\n");
1743 
1744 		fprintf(ofp, "\t\tprint('Sample: {'+"
1745 			"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1746 
1747 		fprintf(ofp, "\t\tfor node in common_callchain:");
1748 		fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1749 		fprintf(ofp, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))");
1750 		fprintf(ofp, "\n\t\t\telse:");
1751 		fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n");
1752 		fprintf(ofp, "\t\tprint()\n\n");
1753 
1754 	}
1755 
1756 	fprintf(ofp, "def trace_unhandled(event_name, context, "
1757 		"event_fields_dict, perf_sample_dict):\n");
1758 
1759 	fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n");
1760 	fprintf(ofp, "\t\tprint('Sample: {'+"
1761 		"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1762 
1763 	fprintf(ofp, "def print_header("
1764 		"event_name, cpu, secs, nsecs, pid, comm):\n"
1765 		"\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1766 		"(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n");
1767 
1768 	fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
1769 		"\treturn delimiter.join"
1770 		"(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
1771 
1772 	fclose(ofp);
1773 
1774 	fprintf(stderr, "generated Python script: %s\n", fname);
1775 
1776 	return 0;
1777 }
1778 
1779 struct scripting_ops python_scripting_ops = {
1780 	.name			= "Python",
1781 	.start_script		= python_start_script,
1782 	.flush_script		= python_flush_script,
1783 	.stop_script		= python_stop_script,
1784 	.process_event		= python_process_event,
1785 	.process_stat		= python_process_stat,
1786 	.process_stat_interval	= python_process_stat_interval,
1787 	.generate_script	= python_generate_script,
1788 };
1789