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