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