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