xref: /linux/tools/perf/util/scripting-engines/trace-event-python.c (revision 4745dc8abb0a0a9851c07265eea01d844886d5c8)
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 "../../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 {
1016 	struct tables *tables = container_of(dbe, struct tables, dbe);
1017 	PyObject *t;
1018 
1019 	t = tuple_new(2);
1020 
1021 	tuple_set_u64(t, 0, comm->db_id);
1022 	tuple_set_string(t, 1, comm__str(comm));
1023 
1024 	call_object(tables->comm_handler, t, "comm_table");
1025 
1026 	Py_DECREF(t);
1027 
1028 	return 0;
1029 }
1030 
1031 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
1032 				     struct comm *comm, struct thread *thread)
1033 {
1034 	struct tables *tables = container_of(dbe, struct tables, dbe);
1035 	PyObject *t;
1036 
1037 	t = tuple_new(3);
1038 
1039 	tuple_set_u64(t, 0, db_id);
1040 	tuple_set_u64(t, 1, comm->db_id);
1041 	tuple_set_u64(t, 2, thread->db_id);
1042 
1043 	call_object(tables->comm_thread_handler, t, "comm_thread_table");
1044 
1045 	Py_DECREF(t);
1046 
1047 	return 0;
1048 }
1049 
1050 static int python_export_dso(struct db_export *dbe, struct dso *dso,
1051 			     struct machine *machine)
1052 {
1053 	struct tables *tables = container_of(dbe, struct tables, dbe);
1054 	char sbuild_id[SBUILD_ID_SIZE];
1055 	PyObject *t;
1056 
1057 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1058 
1059 	t = tuple_new(5);
1060 
1061 	tuple_set_u64(t, 0, dso->db_id);
1062 	tuple_set_u64(t, 1, machine->db_id);
1063 	tuple_set_string(t, 2, dso->short_name);
1064 	tuple_set_string(t, 3, dso->long_name);
1065 	tuple_set_string(t, 4, sbuild_id);
1066 
1067 	call_object(tables->dso_handler, t, "dso_table");
1068 
1069 	Py_DECREF(t);
1070 
1071 	return 0;
1072 }
1073 
1074 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
1075 				struct dso *dso)
1076 {
1077 	struct tables *tables = container_of(dbe, struct tables, dbe);
1078 	u64 *sym_db_id = symbol__priv(sym);
1079 	PyObject *t;
1080 
1081 	t = tuple_new(6);
1082 
1083 	tuple_set_u64(t, 0, *sym_db_id);
1084 	tuple_set_u64(t, 1, dso->db_id);
1085 	tuple_set_u64(t, 2, sym->start);
1086 	tuple_set_u64(t, 3, sym->end);
1087 	tuple_set_s32(t, 4, sym->binding);
1088 	tuple_set_string(t, 5, sym->name);
1089 
1090 	call_object(tables->symbol_handler, t, "symbol_table");
1091 
1092 	Py_DECREF(t);
1093 
1094 	return 0;
1095 }
1096 
1097 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
1098 				     const char *name)
1099 {
1100 	struct tables *tables = container_of(dbe, struct tables, dbe);
1101 	PyObject *t;
1102 
1103 	t = tuple_new(2);
1104 
1105 	tuple_set_s32(t, 0, branch_type);
1106 	tuple_set_string(t, 1, name);
1107 
1108 	call_object(tables->branch_type_handler, t, "branch_type_table");
1109 
1110 	Py_DECREF(t);
1111 
1112 	return 0;
1113 }
1114 
1115 static void python_export_sample_table(struct db_export *dbe,
1116 				       struct export_sample *es)
1117 {
1118 	struct tables *tables = container_of(dbe, struct tables, dbe);
1119 	PyObject *t;
1120 
1121 	t = tuple_new(24);
1122 
1123 	tuple_set_u64(t, 0, es->db_id);
1124 	tuple_set_u64(t, 1, es->evsel->db_id);
1125 	tuple_set_u64(t, 2, es->al->machine->db_id);
1126 	tuple_set_u64(t, 3, es->al->thread->db_id);
1127 	tuple_set_u64(t, 4, es->comm_db_id);
1128 	tuple_set_u64(t, 5, es->dso_db_id);
1129 	tuple_set_u64(t, 6, es->sym_db_id);
1130 	tuple_set_u64(t, 7, es->offset);
1131 	tuple_set_u64(t, 8, es->sample->ip);
1132 	tuple_set_u64(t, 9, es->sample->time);
1133 	tuple_set_s32(t, 10, es->sample->cpu);
1134 	tuple_set_u64(t, 11, es->addr_dso_db_id);
1135 	tuple_set_u64(t, 12, es->addr_sym_db_id);
1136 	tuple_set_u64(t, 13, es->addr_offset);
1137 	tuple_set_u64(t, 14, es->sample->addr);
1138 	tuple_set_u64(t, 15, es->sample->period);
1139 	tuple_set_u64(t, 16, es->sample->weight);
1140 	tuple_set_u64(t, 17, es->sample->transaction);
1141 	tuple_set_u64(t, 18, es->sample->data_src);
1142 	tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
1143 	tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
1144 	tuple_set_u64(t, 21, es->call_path_id);
1145 	tuple_set_u64(t, 22, es->sample->insn_cnt);
1146 	tuple_set_u64(t, 23, es->sample->cyc_cnt);
1147 
1148 	call_object(tables->sample_handler, t, "sample_table");
1149 
1150 	Py_DECREF(t);
1151 }
1152 
1153 static void python_export_synth(struct db_export *dbe, struct export_sample *es)
1154 {
1155 	struct tables *tables = container_of(dbe, struct tables, dbe);
1156 	PyObject *t;
1157 
1158 	t = tuple_new(3);
1159 
1160 	tuple_set_u64(t, 0, es->db_id);
1161 	tuple_set_u64(t, 1, es->evsel->attr.config);
1162 	tuple_set_bytes(t, 2, es->sample->raw_data, es->sample->raw_size);
1163 
1164 	call_object(tables->synth_handler, t, "synth_data");
1165 
1166 	Py_DECREF(t);
1167 }
1168 
1169 static int python_export_sample(struct db_export *dbe,
1170 				struct export_sample *es)
1171 {
1172 	struct tables *tables = container_of(dbe, struct tables, dbe);
1173 
1174 	python_export_sample_table(dbe, es);
1175 
1176 	if (es->evsel->attr.type == PERF_TYPE_SYNTH && tables->synth_handler)
1177 		python_export_synth(dbe, es);
1178 
1179 	return 0;
1180 }
1181 
1182 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
1183 {
1184 	struct tables *tables = container_of(dbe, struct tables, dbe);
1185 	PyObject *t;
1186 	u64 parent_db_id, sym_db_id;
1187 
1188 	parent_db_id = cp->parent ? cp->parent->db_id : 0;
1189 	sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
1190 
1191 	t = tuple_new(4);
1192 
1193 	tuple_set_u64(t, 0, cp->db_id);
1194 	tuple_set_u64(t, 1, parent_db_id);
1195 	tuple_set_u64(t, 2, sym_db_id);
1196 	tuple_set_u64(t, 3, cp->ip);
1197 
1198 	call_object(tables->call_path_handler, t, "call_path_table");
1199 
1200 	Py_DECREF(t);
1201 
1202 	return 0;
1203 }
1204 
1205 static int python_export_call_return(struct db_export *dbe,
1206 				     struct call_return *cr)
1207 {
1208 	struct tables *tables = container_of(dbe, struct tables, dbe);
1209 	u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
1210 	PyObject *t;
1211 
1212 	t = tuple_new(14);
1213 
1214 	tuple_set_u64(t, 0, cr->db_id);
1215 	tuple_set_u64(t, 1, cr->thread->db_id);
1216 	tuple_set_u64(t, 2, comm_db_id);
1217 	tuple_set_u64(t, 3, cr->cp->db_id);
1218 	tuple_set_u64(t, 4, cr->call_time);
1219 	tuple_set_u64(t, 5, cr->return_time);
1220 	tuple_set_u64(t, 6, cr->branch_count);
1221 	tuple_set_u64(t, 7, cr->call_ref);
1222 	tuple_set_u64(t, 8, cr->return_ref);
1223 	tuple_set_u64(t, 9, cr->cp->parent->db_id);
1224 	tuple_set_s32(t, 10, cr->flags);
1225 	tuple_set_u64(t, 11, cr->parent_db_id);
1226 	tuple_set_u64(t, 12, cr->insn_count);
1227 	tuple_set_u64(t, 13, cr->cyc_count);
1228 
1229 	call_object(tables->call_return_handler, t, "call_return_table");
1230 
1231 	Py_DECREF(t);
1232 
1233 	return 0;
1234 }
1235 
1236 static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
1237 				      void *data)
1238 {
1239 	struct db_export *dbe = data;
1240 
1241 	return db_export__call_return(dbe, cr, parent_db_id);
1242 }
1243 
1244 static void python_process_general_event(struct perf_sample *sample,
1245 					 struct perf_evsel *evsel,
1246 					 struct addr_location *al)
1247 {
1248 	PyObject *handler, *t, *dict, *callchain;
1249 	static char handler_name[64];
1250 	unsigned n = 0;
1251 
1252 	snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
1253 
1254 	handler = get_handler(handler_name);
1255 	if (!handler)
1256 		return;
1257 
1258 	/*
1259 	 * Use the MAX_FIELDS to make the function expandable, though
1260 	 * currently there is only one item for the tuple.
1261 	 */
1262 	t = PyTuple_New(MAX_FIELDS);
1263 	if (!t)
1264 		Py_FatalError("couldn't create Python tuple");
1265 
1266 	/* ip unwinding */
1267 	callchain = python_process_callchain(sample, evsel, al);
1268 	dict = get_perf_sample_dict(sample, evsel, al, callchain);
1269 
1270 	PyTuple_SetItem(t, n++, dict);
1271 	if (_PyTuple_Resize(&t, n) == -1)
1272 		Py_FatalError("error resizing Python tuple");
1273 
1274 	call_object(handler, t, handler_name);
1275 
1276 	Py_DECREF(t);
1277 }
1278 
1279 static void python_process_event(union perf_event *event,
1280 				 struct perf_sample *sample,
1281 				 struct perf_evsel *evsel,
1282 				 struct addr_location *al)
1283 {
1284 	struct tables *tables = &tables_global;
1285 
1286 	switch (evsel->attr.type) {
1287 	case PERF_TYPE_TRACEPOINT:
1288 		python_process_tracepoint(sample, evsel, al);
1289 		break;
1290 	/* Reserve for future process_hw/sw/raw APIs */
1291 	default:
1292 		if (tables->db_export_mode)
1293 			db_export__sample(&tables->dbe, event, sample, evsel, al);
1294 		else
1295 			python_process_general_event(sample, evsel, al);
1296 	}
1297 }
1298 
1299 static void get_handler_name(char *str, size_t size,
1300 			     struct perf_evsel *evsel)
1301 {
1302 	char *p = str;
1303 
1304 	scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
1305 
1306 	while ((p = strchr(p, ':'))) {
1307 		*p = '_';
1308 		p++;
1309 	}
1310 }
1311 
1312 static void
1313 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
1314 	     struct perf_counts_values *count)
1315 {
1316 	PyObject *handler, *t;
1317 	static char handler_name[256];
1318 	int n = 0;
1319 
1320 	t = PyTuple_New(MAX_FIELDS);
1321 	if (!t)
1322 		Py_FatalError("couldn't create Python tuple");
1323 
1324 	get_handler_name(handler_name, sizeof(handler_name),
1325 			 counter);
1326 
1327 	handler = get_handler(handler_name);
1328 	if (!handler) {
1329 		pr_debug("can't find python handler %s\n", handler_name);
1330 		return;
1331 	}
1332 
1333 	PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
1334 	PyTuple_SetItem(t, n++, _PyLong_FromLong(thread));
1335 
1336 	tuple_set_u64(t, n++, tstamp);
1337 	tuple_set_u64(t, n++, count->val);
1338 	tuple_set_u64(t, n++, count->ena);
1339 	tuple_set_u64(t, n++, count->run);
1340 
1341 	if (_PyTuple_Resize(&t, n) == -1)
1342 		Py_FatalError("error resizing Python tuple");
1343 
1344 	call_object(handler, t, handler_name);
1345 
1346 	Py_DECREF(t);
1347 }
1348 
1349 static void python_process_stat(struct perf_stat_config *config,
1350 				struct perf_evsel *counter, u64 tstamp)
1351 {
1352 	struct thread_map *threads = counter->threads;
1353 	struct cpu_map *cpus = counter->cpus;
1354 	int cpu, thread;
1355 
1356 	if (config->aggr_mode == AGGR_GLOBAL) {
1357 		process_stat(counter, -1, -1, tstamp,
1358 			     &counter->counts->aggr);
1359 		return;
1360 	}
1361 
1362 	for (thread = 0; thread < threads->nr; thread++) {
1363 		for (cpu = 0; cpu < cpus->nr; cpu++) {
1364 			process_stat(counter, cpus->map[cpu],
1365 				     thread_map__pid(threads, thread), tstamp,
1366 				     perf_counts(counter->counts, cpu, thread));
1367 		}
1368 	}
1369 }
1370 
1371 static void python_process_stat_interval(u64 tstamp)
1372 {
1373 	PyObject *handler, *t;
1374 	static const char handler_name[] = "stat__interval";
1375 	int n = 0;
1376 
1377 	t = PyTuple_New(MAX_FIELDS);
1378 	if (!t)
1379 		Py_FatalError("couldn't create Python tuple");
1380 
1381 	handler = get_handler(handler_name);
1382 	if (!handler) {
1383 		pr_debug("can't find python handler %s\n", handler_name);
1384 		return;
1385 	}
1386 
1387 	tuple_set_u64(t, n++, tstamp);
1388 
1389 	if (_PyTuple_Resize(&t, n) == -1)
1390 		Py_FatalError("error resizing Python tuple");
1391 
1392 	call_object(handler, t, handler_name);
1393 
1394 	Py_DECREF(t);
1395 }
1396 
1397 static int run_start_sub(void)
1398 {
1399 	main_module = PyImport_AddModule("__main__");
1400 	if (main_module == NULL)
1401 		return -1;
1402 	Py_INCREF(main_module);
1403 
1404 	main_dict = PyModule_GetDict(main_module);
1405 	if (main_dict == NULL)
1406 		goto error;
1407 	Py_INCREF(main_dict);
1408 
1409 	try_call_object("trace_begin", NULL);
1410 
1411 	return 0;
1412 
1413 error:
1414 	Py_XDECREF(main_dict);
1415 	Py_XDECREF(main_module);
1416 	return -1;
1417 }
1418 
1419 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do {		\
1420 	tables->handler_name = get_handler(#table_name);		\
1421 	if (tables->handler_name)					\
1422 		tables->dbe.export_ ## name = python_export_ ## name;	\
1423 } while (0)
1424 
1425 #define SET_TABLE_HANDLER(name) \
1426 	SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1427 
1428 static void set_table_handlers(struct tables *tables)
1429 {
1430 	const char *perf_db_export_mode = "perf_db_export_mode";
1431 	const char *perf_db_export_calls = "perf_db_export_calls";
1432 	const char *perf_db_export_callchains = "perf_db_export_callchains";
1433 	PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1434 	bool export_calls = false;
1435 	bool export_callchains = false;
1436 	int ret;
1437 
1438 	memset(tables, 0, sizeof(struct tables));
1439 	if (db_export__init(&tables->dbe))
1440 		Py_FatalError("failed to initialize export");
1441 
1442 	db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1443 	if (!db_export_mode)
1444 		return;
1445 
1446 	ret = PyObject_IsTrue(db_export_mode);
1447 	if (ret == -1)
1448 		handler_call_die(perf_db_export_mode);
1449 	if (!ret)
1450 		return;
1451 
1452 	/* handle export calls */
1453 	tables->dbe.crp = NULL;
1454 	db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1455 	if (db_export_calls) {
1456 		ret = PyObject_IsTrue(db_export_calls);
1457 		if (ret == -1)
1458 			handler_call_die(perf_db_export_calls);
1459 		export_calls = !!ret;
1460 	}
1461 
1462 	if (export_calls) {
1463 		tables->dbe.crp =
1464 			call_return_processor__new(python_process_call_return,
1465 						   &tables->dbe);
1466 		if (!tables->dbe.crp)
1467 			Py_FatalError("failed to create calls processor");
1468 	}
1469 
1470 	/* handle export callchains */
1471 	tables->dbe.cpr = NULL;
1472 	db_export_callchains = PyDict_GetItemString(main_dict,
1473 						    perf_db_export_callchains);
1474 	if (db_export_callchains) {
1475 		ret = PyObject_IsTrue(db_export_callchains);
1476 		if (ret == -1)
1477 			handler_call_die(perf_db_export_callchains);
1478 		export_callchains = !!ret;
1479 	}
1480 
1481 	if (export_callchains) {
1482 		/*
1483 		 * Attempt to use the call path root from the call return
1484 		 * processor, if the call return processor is in use. Otherwise,
1485 		 * we allocate a new call path root. This prevents exporting
1486 		 * duplicate call path ids when both are in use simultaniously.
1487 		 */
1488 		if (tables->dbe.crp)
1489 			tables->dbe.cpr = tables->dbe.crp->cpr;
1490 		else
1491 			tables->dbe.cpr = call_path_root__new();
1492 
1493 		if (!tables->dbe.cpr)
1494 			Py_FatalError("failed to create call path root");
1495 	}
1496 
1497 	tables->db_export_mode = true;
1498 	/*
1499 	 * Reserve per symbol space for symbol->db_id via symbol__priv()
1500 	 */
1501 	symbol_conf.priv_size = sizeof(u64);
1502 
1503 	SET_TABLE_HANDLER(evsel);
1504 	SET_TABLE_HANDLER(machine);
1505 	SET_TABLE_HANDLER(thread);
1506 	SET_TABLE_HANDLER(comm);
1507 	SET_TABLE_HANDLER(comm_thread);
1508 	SET_TABLE_HANDLER(dso);
1509 	SET_TABLE_HANDLER(symbol);
1510 	SET_TABLE_HANDLER(branch_type);
1511 	SET_TABLE_HANDLER(sample);
1512 	SET_TABLE_HANDLER(call_path);
1513 	SET_TABLE_HANDLER(call_return);
1514 
1515 	/*
1516 	 * Synthesized events are samples but with architecture-specific data
1517 	 * stored in sample->raw_data. They are exported via
1518 	 * python_export_sample() and consequently do not need a separate export
1519 	 * callback.
1520 	 */
1521 	tables->synth_handler = get_handler("synth_data");
1522 }
1523 
1524 #if PY_MAJOR_VERSION < 3
1525 static void _free_command_line(const char **command_line, int num)
1526 {
1527 	free(command_line);
1528 }
1529 #else
1530 static void _free_command_line(wchar_t **command_line, int num)
1531 {
1532 	int i;
1533 	for (i = 0; i < num; i++)
1534 		PyMem_RawFree(command_line[i]);
1535 	free(command_line);
1536 }
1537 #endif
1538 
1539 
1540 /*
1541  * Start trace script
1542  */
1543 static int python_start_script(const char *script, int argc, const char **argv)
1544 {
1545 	struct tables *tables = &tables_global;
1546 	PyMODINIT_FUNC (*initfunc)(void);
1547 #if PY_MAJOR_VERSION < 3
1548 	const char **command_line;
1549 #else
1550 	wchar_t **command_line;
1551 #endif
1552 	/*
1553 	 * Use a non-const name variable to cope with python 2.6's
1554 	 * PyImport_AppendInittab prototype
1555 	 */
1556 	char buf[PATH_MAX], name[19] = "perf_trace_context";
1557 	int i, err = 0;
1558 	FILE *fp;
1559 
1560 #if PY_MAJOR_VERSION < 3
1561 	initfunc = initperf_trace_context;
1562 	command_line = malloc((argc + 1) * sizeof(const char *));
1563 	command_line[0] = script;
1564 	for (i = 1; i < argc + 1; i++)
1565 		command_line[i] = argv[i - 1];
1566 #else
1567 	initfunc = PyInit_perf_trace_context;
1568 	command_line = malloc((argc + 1) * sizeof(wchar_t *));
1569 	command_line[0] = Py_DecodeLocale(script, NULL);
1570 	for (i = 1; i < argc + 1; i++)
1571 		command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
1572 #endif
1573 
1574 	PyImport_AppendInittab(name, initfunc);
1575 	Py_Initialize();
1576 
1577 #if PY_MAJOR_VERSION < 3
1578 	PySys_SetArgv(argc + 1, (char **)command_line);
1579 #else
1580 	PySys_SetArgv(argc + 1, command_line);
1581 #endif
1582 
1583 	fp = fopen(script, "r");
1584 	if (!fp) {
1585 		sprintf(buf, "Can't open python script \"%s\"", script);
1586 		perror(buf);
1587 		err = -1;
1588 		goto error;
1589 	}
1590 
1591 	err = PyRun_SimpleFile(fp, script);
1592 	if (err) {
1593 		fprintf(stderr, "Error running python script %s\n", script);
1594 		goto error;
1595 	}
1596 
1597 	err = run_start_sub();
1598 	if (err) {
1599 		fprintf(stderr, "Error starting python script %s\n", script);
1600 		goto error;
1601 	}
1602 
1603 	set_table_handlers(tables);
1604 
1605 	if (tables->db_export_mode) {
1606 		err = db_export__branch_types(&tables->dbe);
1607 		if (err)
1608 			goto error;
1609 	}
1610 
1611 	_free_command_line(command_line, argc + 1);
1612 
1613 	return err;
1614 error:
1615 	Py_Finalize();
1616 	_free_command_line(command_line, argc + 1);
1617 
1618 	return err;
1619 }
1620 
1621 static int python_flush_script(void)
1622 {
1623 	struct tables *tables = &tables_global;
1624 
1625 	return db_export__flush(&tables->dbe);
1626 }
1627 
1628 /*
1629  * Stop trace script
1630  */
1631 static int python_stop_script(void)
1632 {
1633 	struct tables *tables = &tables_global;
1634 
1635 	try_call_object("trace_end", NULL);
1636 
1637 	db_export__exit(&tables->dbe);
1638 
1639 	Py_XDECREF(main_dict);
1640 	Py_XDECREF(main_module);
1641 	Py_Finalize();
1642 
1643 	return 0;
1644 }
1645 
1646 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
1647 {
1648 	struct tep_event *event = NULL;
1649 	struct tep_format_field *f;
1650 	char fname[PATH_MAX];
1651 	int not_first, count;
1652 	FILE *ofp;
1653 
1654 	sprintf(fname, "%s.py", outfile);
1655 	ofp = fopen(fname, "w");
1656 	if (ofp == NULL) {
1657 		fprintf(stderr, "couldn't open %s\n", fname);
1658 		return -1;
1659 	}
1660 	fprintf(ofp, "# perf script event handlers, "
1661 		"generated by perf script -g python\n");
1662 
1663 	fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1664 		" License version 2\n\n");
1665 
1666 	fprintf(ofp, "# The common_* event handler fields are the most useful "
1667 		"fields common to\n");
1668 
1669 	fprintf(ofp, "# all events.  They don't necessarily correspond to "
1670 		"the 'common_*' fields\n");
1671 
1672 	fprintf(ofp, "# in the format files.  Those fields not available as "
1673 		"handler params can\n");
1674 
1675 	fprintf(ofp, "# be retrieved using Python functions of the form "
1676 		"common_*(context).\n");
1677 
1678 	fprintf(ofp, "# See the perf-script-python Documentation for the list "
1679 		"of available functions.\n\n");
1680 
1681 	fprintf(ofp, "from __future__ import print_function\n\n");
1682 	fprintf(ofp, "import os\n");
1683 	fprintf(ofp, "import sys\n\n");
1684 
1685 	fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1686 	fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1687 	fprintf(ofp, "\nfrom perf_trace_context import *\n");
1688 	fprintf(ofp, "from Core import *\n\n\n");
1689 
1690 	fprintf(ofp, "def trace_begin():\n");
1691 	fprintf(ofp, "\tprint(\"in trace_begin\")\n\n");
1692 
1693 	fprintf(ofp, "def trace_end():\n");
1694 	fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
1695 
1696 	while ((event = trace_find_next_event(pevent, event))) {
1697 		fprintf(ofp, "def %s__%s(", event->system, event->name);
1698 		fprintf(ofp, "event_name, ");
1699 		fprintf(ofp, "context, ");
1700 		fprintf(ofp, "common_cpu,\n");
1701 		fprintf(ofp, "\tcommon_secs, ");
1702 		fprintf(ofp, "common_nsecs, ");
1703 		fprintf(ofp, "common_pid, ");
1704 		fprintf(ofp, "common_comm,\n\t");
1705 		fprintf(ofp, "common_callchain, ");
1706 
1707 		not_first = 0;
1708 		count = 0;
1709 
1710 		for (f = event->format.fields; f; f = f->next) {
1711 			if (not_first++)
1712 				fprintf(ofp, ", ");
1713 			if (++count % 5 == 0)
1714 				fprintf(ofp, "\n\t");
1715 
1716 			fprintf(ofp, "%s", f->name);
1717 		}
1718 		if (not_first++)
1719 			fprintf(ofp, ", ");
1720 		if (++count % 5 == 0)
1721 			fprintf(ofp, "\n\t\t");
1722 		fprintf(ofp, "perf_sample_dict");
1723 
1724 		fprintf(ofp, "):\n");
1725 
1726 		fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1727 			"common_secs, common_nsecs,\n\t\t\t"
1728 			"common_pid, common_comm)\n\n");
1729 
1730 		fprintf(ofp, "\t\tprint(\"");
1731 
1732 		not_first = 0;
1733 		count = 0;
1734 
1735 		for (f = event->format.fields; f; f = f->next) {
1736 			if (not_first++)
1737 				fprintf(ofp, ", ");
1738 			if (count && count % 3 == 0) {
1739 				fprintf(ofp, "\" \\\n\t\t\"");
1740 			}
1741 			count++;
1742 
1743 			fprintf(ofp, "%s=", f->name);
1744 			if (f->flags & TEP_FIELD_IS_STRING ||
1745 			    f->flags & TEP_FIELD_IS_FLAG ||
1746 			    f->flags & TEP_FIELD_IS_ARRAY ||
1747 			    f->flags & TEP_FIELD_IS_SYMBOLIC)
1748 				fprintf(ofp, "%%s");
1749 			else if (f->flags & TEP_FIELD_IS_SIGNED)
1750 				fprintf(ofp, "%%d");
1751 			else
1752 				fprintf(ofp, "%%u");
1753 		}
1754 
1755 		fprintf(ofp, "\" %% \\\n\t\t(");
1756 
1757 		not_first = 0;
1758 		count = 0;
1759 
1760 		for (f = event->format.fields; f; f = f->next) {
1761 			if (not_first++)
1762 				fprintf(ofp, ", ");
1763 
1764 			if (++count % 5 == 0)
1765 				fprintf(ofp, "\n\t\t");
1766 
1767 			if (f->flags & TEP_FIELD_IS_FLAG) {
1768 				if ((count - 1) % 5 != 0) {
1769 					fprintf(ofp, "\n\t\t");
1770 					count = 4;
1771 				}
1772 				fprintf(ofp, "flag_str(\"");
1773 				fprintf(ofp, "%s__%s\", ", event->system,
1774 					event->name);
1775 				fprintf(ofp, "\"%s\", %s)", f->name,
1776 					f->name);
1777 			} else if (f->flags & TEP_FIELD_IS_SYMBOLIC) {
1778 				if ((count - 1) % 5 != 0) {
1779 					fprintf(ofp, "\n\t\t");
1780 					count = 4;
1781 				}
1782 				fprintf(ofp, "symbol_str(\"");
1783 				fprintf(ofp, "%s__%s\", ", event->system,
1784 					event->name);
1785 				fprintf(ofp, "\"%s\", %s)", f->name,
1786 					f->name);
1787 			} else
1788 				fprintf(ofp, "%s", f->name);
1789 		}
1790 
1791 		fprintf(ofp, "))\n\n");
1792 
1793 		fprintf(ofp, "\t\tprint('Sample: {'+"
1794 			"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1795 
1796 		fprintf(ofp, "\t\tfor node in common_callchain:");
1797 		fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1798 		fprintf(ofp, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))");
1799 		fprintf(ofp, "\n\t\t\telse:");
1800 		fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n");
1801 		fprintf(ofp, "\t\tprint()\n\n");
1802 
1803 	}
1804 
1805 	fprintf(ofp, "def trace_unhandled(event_name, context, "
1806 		"event_fields_dict, perf_sample_dict):\n");
1807 
1808 	fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n");
1809 	fprintf(ofp, "\t\tprint('Sample: {'+"
1810 		"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
1811 
1812 	fprintf(ofp, "def print_header("
1813 		"event_name, cpu, secs, nsecs, pid, comm):\n"
1814 		"\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1815 		"(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n");
1816 
1817 	fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
1818 		"\treturn delimiter.join"
1819 		"(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
1820 
1821 	fclose(ofp);
1822 
1823 	fprintf(stderr, "generated Python script: %s\n", fname);
1824 
1825 	return 0;
1826 }
1827 
1828 struct scripting_ops python_scripting_ops = {
1829 	.name			= "Python",
1830 	.start_script		= python_start_script,
1831 	.flush_script		= python_flush_script,
1832 	.stop_script		= python_stop_script,
1833 	.process_event		= python_process_event,
1834 	.process_stat		= python_process_stat,
1835 	.process_stat_interval	= python_process_stat_interval,
1836 	.generate_script	= python_generate_script,
1837 };
1838