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