xref: /linux/tools/perf/util/scripting-engines/trace-event-python.c (revision 975f14fa8f2996604f248552eee4403def34bf5e)
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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdbool.h>
28 #include <errno.h>
29 #include <linux/bitmap.h>
30 
31 #include "../../perf.h"
32 #include "../debug.h"
33 #include "../callchain.h"
34 #include "../evsel.h"
35 #include "../util.h"
36 #include "../event.h"
37 #include "../thread.h"
38 #include "../comm.h"
39 #include "../machine.h"
40 #include "../db-export.h"
41 #include "../thread-stack.h"
42 #include "../trace-event.h"
43 #include "../machine.h"
44 #include "thread_map.h"
45 #include "cpumap.h"
46 #include "stat.h"
47 
48 PyMODINIT_FUNC initperf_trace_context(void);
49 
50 #define TRACE_EVENT_TYPE_MAX				\
51 	((1 << (sizeof(unsigned short) * 8)) - 1)
52 
53 static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
54 
55 #define MAX_FIELDS	64
56 #define N_COMMON_FIELDS	7
57 
58 extern struct scripting_context *scripting_context;
59 
60 static char *cur_field_name;
61 static int zero_flag_atom;
62 
63 static PyObject *main_module, *main_dict;
64 
65 struct tables {
66 	struct db_export	dbe;
67 	PyObject		*evsel_handler;
68 	PyObject		*machine_handler;
69 	PyObject		*thread_handler;
70 	PyObject		*comm_handler;
71 	PyObject		*comm_thread_handler;
72 	PyObject		*dso_handler;
73 	PyObject		*symbol_handler;
74 	PyObject		*branch_type_handler;
75 	PyObject		*sample_handler;
76 	PyObject		*call_path_handler;
77 	PyObject		*call_return_handler;
78 	bool			db_export_mode;
79 };
80 
81 static struct tables tables_global;
82 
83 static void handler_call_die(const char *handler_name) NORETURN;
84 static void handler_call_die(const char *handler_name)
85 {
86 	PyErr_Print();
87 	Py_FatalError("problem in Python trace event handler");
88 	// Py_FatalError does not return
89 	// but we have to make the compiler happy
90 	abort();
91 }
92 
93 /*
94  * Insert val into into the dictionary and decrement the reference counter.
95  * This is necessary for dictionaries since PyDict_SetItemString() does not
96  * steal a reference, as opposed to PyTuple_SetItem().
97  */
98 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
99 {
100 	PyDict_SetItemString(dict, key, val);
101 	Py_DECREF(val);
102 }
103 
104 static PyObject *get_handler(const char *handler_name)
105 {
106 	PyObject *handler;
107 
108 	handler = PyDict_GetItemString(main_dict, handler_name);
109 	if (handler && !PyCallable_Check(handler))
110 		return NULL;
111 	return handler;
112 }
113 
114 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
115 {
116 	PyObject *retval;
117 
118 	retval = PyObject_CallObject(handler, args);
119 	if (retval == NULL)
120 		handler_call_die(die_msg);
121 	Py_DECREF(retval);
122 }
123 
124 static void try_call_object(const char *handler_name, PyObject *args)
125 {
126 	PyObject *handler;
127 
128 	handler = get_handler(handler_name);
129 	if (handler)
130 		call_object(handler, args, handler_name);
131 }
132 
133 static void define_value(enum print_arg_type field_type,
134 			 const char *ev_name,
135 			 const char *field_name,
136 			 const char *field_value,
137 			 const char *field_str)
138 {
139 	const char *handler_name = "define_flag_value";
140 	PyObject *t;
141 	unsigned long long value;
142 	unsigned n = 0;
143 
144 	if (field_type == PRINT_SYMBOL)
145 		handler_name = "define_symbolic_value";
146 
147 	t = PyTuple_New(4);
148 	if (!t)
149 		Py_FatalError("couldn't create Python tuple");
150 
151 	value = eval_flag(field_value);
152 
153 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
154 	PyTuple_SetItem(t, n++, PyString_FromString(field_name));
155 	PyTuple_SetItem(t, n++, PyInt_FromLong(value));
156 	PyTuple_SetItem(t, n++, PyString_FromString(field_str));
157 
158 	try_call_object(handler_name, t);
159 
160 	Py_DECREF(t);
161 }
162 
163 static void define_values(enum print_arg_type field_type,
164 			  struct print_flag_sym *field,
165 			  const char *ev_name,
166 			  const char *field_name)
167 {
168 	define_value(field_type, ev_name, field_name, field->value,
169 		     field->str);
170 
171 	if (field->next)
172 		define_values(field_type, field->next, ev_name, field_name);
173 }
174 
175 static void define_field(enum print_arg_type field_type,
176 			 const char *ev_name,
177 			 const char *field_name,
178 			 const char *delim)
179 {
180 	const char *handler_name = "define_flag_field";
181 	PyObject *t;
182 	unsigned n = 0;
183 
184 	if (field_type == PRINT_SYMBOL)
185 		handler_name = "define_symbolic_field";
186 
187 	if (field_type == PRINT_FLAGS)
188 		t = PyTuple_New(3);
189 	else
190 		t = PyTuple_New(2);
191 	if (!t)
192 		Py_FatalError("couldn't create Python tuple");
193 
194 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
195 	PyTuple_SetItem(t, n++, PyString_FromString(field_name));
196 	if (field_type == PRINT_FLAGS)
197 		PyTuple_SetItem(t, n++, PyString_FromString(delim));
198 
199 	try_call_object(handler_name, t);
200 
201 	Py_DECREF(t);
202 }
203 
204 static void define_event_symbols(struct event_format *event,
205 				 const char *ev_name,
206 				 struct print_arg *args)
207 {
208 	switch (args->type) {
209 	case PRINT_NULL:
210 		break;
211 	case PRINT_ATOM:
212 		define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
213 			     args->atom.atom);
214 		zero_flag_atom = 0;
215 		break;
216 	case PRINT_FIELD:
217 		free(cur_field_name);
218 		cur_field_name = strdup(args->field.name);
219 		break;
220 	case PRINT_FLAGS:
221 		define_event_symbols(event, ev_name, args->flags.field);
222 		define_field(PRINT_FLAGS, ev_name, cur_field_name,
223 			     args->flags.delim);
224 		define_values(PRINT_FLAGS, args->flags.flags, ev_name,
225 			      cur_field_name);
226 		break;
227 	case PRINT_SYMBOL:
228 		define_event_symbols(event, ev_name, args->symbol.field);
229 		define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
230 		define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
231 			      cur_field_name);
232 		break;
233 	case PRINT_HEX:
234 		define_event_symbols(event, ev_name, args->hex.field);
235 		define_event_symbols(event, ev_name, args->hex.size);
236 		break;
237 	case PRINT_INT_ARRAY:
238 		define_event_symbols(event, ev_name, args->int_array.field);
239 		define_event_symbols(event, ev_name, args->int_array.count);
240 		define_event_symbols(event, ev_name, args->int_array.el_size);
241 		break;
242 	case PRINT_STRING:
243 		break;
244 	case PRINT_TYPE:
245 		define_event_symbols(event, ev_name, args->typecast.item);
246 		break;
247 	case PRINT_OP:
248 		if (strcmp(args->op.op, ":") == 0)
249 			zero_flag_atom = 1;
250 		define_event_symbols(event, ev_name, args->op.left);
251 		define_event_symbols(event, ev_name, args->op.right);
252 		break;
253 	default:
254 		/* gcc warns for these? */
255 	case PRINT_BSTRING:
256 	case PRINT_DYNAMIC_ARRAY:
257 	case PRINT_DYNAMIC_ARRAY_LEN:
258 	case PRINT_FUNC:
259 	case PRINT_BITMASK:
260 		/* we should warn... */
261 		return;
262 	}
263 
264 	if (args->next)
265 		define_event_symbols(event, ev_name, args->next);
266 }
267 
268 static PyObject *get_field_numeric_entry(struct event_format *event,
269 		struct format_field *field, void *data)
270 {
271 	bool is_array = field->flags & FIELD_IS_ARRAY;
272 	PyObject *obj, *list = NULL;
273 	unsigned long long val;
274 	unsigned int item_size, n_items, i;
275 
276 	if (is_array) {
277 		list = PyList_New(field->arraylen);
278 		item_size = field->size / field->arraylen;
279 		n_items = field->arraylen;
280 	} else {
281 		item_size = field->size;
282 		n_items = 1;
283 	}
284 
285 	for (i = 0; i < n_items; i++) {
286 
287 		val = read_size(event, data + field->offset + i * item_size,
288 				item_size);
289 		if (field->flags & FIELD_IS_SIGNED) {
290 			if ((long long)val >= LONG_MIN &&
291 					(long long)val <= LONG_MAX)
292 				obj = PyInt_FromLong(val);
293 			else
294 				obj = PyLong_FromLongLong(val);
295 		} else {
296 			if (val <= LONG_MAX)
297 				obj = PyInt_FromLong(val);
298 			else
299 				obj = PyLong_FromUnsignedLongLong(val);
300 		}
301 		if (is_array)
302 			PyList_SET_ITEM(list, i, obj);
303 	}
304 	if (is_array)
305 		obj = list;
306 	return obj;
307 }
308 
309 
310 static PyObject *python_process_callchain(struct perf_sample *sample,
311 					 struct perf_evsel *evsel,
312 					 struct addr_location *al)
313 {
314 	PyObject *pylist;
315 
316 	pylist = PyList_New(0);
317 	if (!pylist)
318 		Py_FatalError("couldn't create Python list");
319 
320 	if (!symbol_conf.use_callchain || !sample->callchain)
321 		goto exit;
322 
323 	if (thread__resolve_callchain(al->thread, evsel,
324 				      sample, NULL, NULL,
325 				      scripting_max_stack) != 0) {
326 		pr_err("Failed to resolve callchain. Skipping\n");
327 		goto exit;
328 	}
329 	callchain_cursor_commit(&callchain_cursor);
330 
331 
332 	while (1) {
333 		PyObject *pyelem;
334 		struct callchain_cursor_node *node;
335 		node = callchain_cursor_current(&callchain_cursor);
336 		if (!node)
337 			break;
338 
339 		pyelem = PyDict_New();
340 		if (!pyelem)
341 			Py_FatalError("couldn't create Python dictionary");
342 
343 
344 		pydict_set_item_string_decref(pyelem, "ip",
345 				PyLong_FromUnsignedLongLong(node->ip));
346 
347 		if (node->sym) {
348 			PyObject *pysym  = PyDict_New();
349 			if (!pysym)
350 				Py_FatalError("couldn't create Python dictionary");
351 			pydict_set_item_string_decref(pysym, "start",
352 					PyLong_FromUnsignedLongLong(node->sym->start));
353 			pydict_set_item_string_decref(pysym, "end",
354 					PyLong_FromUnsignedLongLong(node->sym->end));
355 			pydict_set_item_string_decref(pysym, "binding",
356 					PyInt_FromLong(node->sym->binding));
357 			pydict_set_item_string_decref(pysym, "name",
358 					PyString_FromStringAndSize(node->sym->name,
359 							node->sym->namelen));
360 			pydict_set_item_string_decref(pyelem, "sym", pysym);
361 		}
362 
363 		if (node->map) {
364 			struct map *map = node->map;
365 			const char *dsoname = "[unknown]";
366 			if (map && map->dso && (map->dso->name || map->dso->long_name)) {
367 				if (symbol_conf.show_kernel_path && map->dso->long_name)
368 					dsoname = map->dso->long_name;
369 				else if (map->dso->name)
370 					dsoname = map->dso->name;
371 			}
372 			pydict_set_item_string_decref(pyelem, "dso",
373 					PyString_FromString(dsoname));
374 		}
375 
376 		callchain_cursor_advance(&callchain_cursor);
377 		PyList_Append(pylist, pyelem);
378 		Py_DECREF(pyelem);
379 	}
380 
381 exit:
382 	return pylist;
383 }
384 
385 
386 static void python_process_tracepoint(struct perf_sample *sample,
387 				      struct perf_evsel *evsel,
388 				      struct addr_location *al)
389 {
390 	struct event_format *event = evsel->tp_format;
391 	PyObject *handler, *context, *t, *obj, *callchain;
392 	PyObject *dict = NULL;
393 	static char handler_name[256];
394 	struct format_field *field;
395 	unsigned long s, ns;
396 	unsigned n = 0;
397 	int pid;
398 	int cpu = sample->cpu;
399 	void *data = sample->raw_data;
400 	unsigned long long nsecs = sample->time;
401 	const char *comm = thread__comm_str(al->thread);
402 
403 	t = PyTuple_New(MAX_FIELDS);
404 	if (!t)
405 		Py_FatalError("couldn't create Python tuple");
406 
407 	if (!event)
408 		die("ug! no event found for type %d", (int)evsel->attr.config);
409 
410 	pid = raw_field_value(event, "common_pid", data);
411 
412 	sprintf(handler_name, "%s__%s", event->system, event->name);
413 
414 	if (!test_and_set_bit(event->id, events_defined))
415 		define_event_symbols(event, handler_name, event->print_fmt.args);
416 
417 	handler = get_handler(handler_name);
418 	if (!handler) {
419 		dict = PyDict_New();
420 		if (!dict)
421 			Py_FatalError("couldn't create Python dict");
422 	}
423 	s = nsecs / NSECS_PER_SEC;
424 	ns = nsecs - s * NSECS_PER_SEC;
425 
426 	scripting_context->event_data = data;
427 	scripting_context->pevent = evsel->tp_format->pevent;
428 
429 	context = PyCObject_FromVoidPtr(scripting_context, NULL);
430 
431 	PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
432 	PyTuple_SetItem(t, n++, context);
433 
434 	/* ip unwinding */
435 	callchain = python_process_callchain(sample, evsel, al);
436 
437 	if (handler) {
438 		PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
439 		PyTuple_SetItem(t, n++, PyInt_FromLong(s));
440 		PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
441 		PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
442 		PyTuple_SetItem(t, n++, PyString_FromString(comm));
443 		PyTuple_SetItem(t, n++, callchain);
444 	} else {
445 		pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
446 		pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
447 		pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
448 		pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
449 		pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
450 		pydict_set_item_string_decref(dict, "common_callchain", callchain);
451 	}
452 	for (field = event->format.fields; field; field = field->next) {
453 		if (field->flags & FIELD_IS_STRING) {
454 			int offset;
455 			if (field->flags & FIELD_IS_DYNAMIC) {
456 				offset = *(int *)(data + field->offset);
457 				offset &= 0xffff;
458 			} else
459 				offset = field->offset;
460 			obj = PyString_FromString((char *)data + offset);
461 		} else { /* FIELD_IS_NUMERIC */
462 			obj = get_field_numeric_entry(event, field, data);
463 		}
464 		if (handler)
465 			PyTuple_SetItem(t, n++, obj);
466 		else
467 			pydict_set_item_string_decref(dict, field->name, obj);
468 
469 	}
470 
471 	if (!handler)
472 		PyTuple_SetItem(t, n++, dict);
473 
474 	if (_PyTuple_Resize(&t, n) == -1)
475 		Py_FatalError("error resizing Python tuple");
476 
477 	if (handler) {
478 		call_object(handler, t, handler_name);
479 	} else {
480 		try_call_object("trace_unhandled", t);
481 		Py_DECREF(dict);
482 	}
483 
484 	Py_DECREF(t);
485 }
486 
487 static PyObject *tuple_new(unsigned int sz)
488 {
489 	PyObject *t;
490 
491 	t = PyTuple_New(sz);
492 	if (!t)
493 		Py_FatalError("couldn't create Python tuple");
494 	return t;
495 }
496 
497 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
498 {
499 #if BITS_PER_LONG == 64
500 	return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
501 #endif
502 #if BITS_PER_LONG == 32
503 	return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
504 #endif
505 }
506 
507 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
508 {
509 	return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
510 }
511 
512 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
513 {
514 	return PyTuple_SetItem(t, pos, PyString_FromString(s));
515 }
516 
517 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
518 {
519 	struct tables *tables = container_of(dbe, struct tables, dbe);
520 	PyObject *t;
521 
522 	t = tuple_new(2);
523 
524 	tuple_set_u64(t, 0, evsel->db_id);
525 	tuple_set_string(t, 1, perf_evsel__name(evsel));
526 
527 	call_object(tables->evsel_handler, t, "evsel_table");
528 
529 	Py_DECREF(t);
530 
531 	return 0;
532 }
533 
534 static int python_export_machine(struct db_export *dbe,
535 				 struct machine *machine)
536 {
537 	struct tables *tables = container_of(dbe, struct tables, dbe);
538 	PyObject *t;
539 
540 	t = tuple_new(3);
541 
542 	tuple_set_u64(t, 0, machine->db_id);
543 	tuple_set_s32(t, 1, machine->pid);
544 	tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
545 
546 	call_object(tables->machine_handler, t, "machine_table");
547 
548 	Py_DECREF(t);
549 
550 	return 0;
551 }
552 
553 static int python_export_thread(struct db_export *dbe, struct thread *thread,
554 				u64 main_thread_db_id, struct machine *machine)
555 {
556 	struct tables *tables = container_of(dbe, struct tables, dbe);
557 	PyObject *t;
558 
559 	t = tuple_new(5);
560 
561 	tuple_set_u64(t, 0, thread->db_id);
562 	tuple_set_u64(t, 1, machine->db_id);
563 	tuple_set_u64(t, 2, main_thread_db_id);
564 	tuple_set_s32(t, 3, thread->pid_);
565 	tuple_set_s32(t, 4, thread->tid);
566 
567 	call_object(tables->thread_handler, t, "thread_table");
568 
569 	Py_DECREF(t);
570 
571 	return 0;
572 }
573 
574 static int python_export_comm(struct db_export *dbe, struct comm *comm)
575 {
576 	struct tables *tables = container_of(dbe, struct tables, dbe);
577 	PyObject *t;
578 
579 	t = tuple_new(2);
580 
581 	tuple_set_u64(t, 0, comm->db_id);
582 	tuple_set_string(t, 1, comm__str(comm));
583 
584 	call_object(tables->comm_handler, t, "comm_table");
585 
586 	Py_DECREF(t);
587 
588 	return 0;
589 }
590 
591 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
592 				     struct comm *comm, struct thread *thread)
593 {
594 	struct tables *tables = container_of(dbe, struct tables, dbe);
595 	PyObject *t;
596 
597 	t = tuple_new(3);
598 
599 	tuple_set_u64(t, 0, db_id);
600 	tuple_set_u64(t, 1, comm->db_id);
601 	tuple_set_u64(t, 2, thread->db_id);
602 
603 	call_object(tables->comm_thread_handler, t, "comm_thread_table");
604 
605 	Py_DECREF(t);
606 
607 	return 0;
608 }
609 
610 static int python_export_dso(struct db_export *dbe, struct dso *dso,
611 			     struct machine *machine)
612 {
613 	struct tables *tables = container_of(dbe, struct tables, dbe);
614 	char sbuild_id[BUILD_ID_SIZE * 2 + 1];
615 	PyObject *t;
616 
617 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
618 
619 	t = tuple_new(5);
620 
621 	tuple_set_u64(t, 0, dso->db_id);
622 	tuple_set_u64(t, 1, machine->db_id);
623 	tuple_set_string(t, 2, dso->short_name);
624 	tuple_set_string(t, 3, dso->long_name);
625 	tuple_set_string(t, 4, sbuild_id);
626 
627 	call_object(tables->dso_handler, t, "dso_table");
628 
629 	Py_DECREF(t);
630 
631 	return 0;
632 }
633 
634 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
635 				struct dso *dso)
636 {
637 	struct tables *tables = container_of(dbe, struct tables, dbe);
638 	u64 *sym_db_id = symbol__priv(sym);
639 	PyObject *t;
640 
641 	t = tuple_new(6);
642 
643 	tuple_set_u64(t, 0, *sym_db_id);
644 	tuple_set_u64(t, 1, dso->db_id);
645 	tuple_set_u64(t, 2, sym->start);
646 	tuple_set_u64(t, 3, sym->end);
647 	tuple_set_s32(t, 4, sym->binding);
648 	tuple_set_string(t, 5, sym->name);
649 
650 	call_object(tables->symbol_handler, t, "symbol_table");
651 
652 	Py_DECREF(t);
653 
654 	return 0;
655 }
656 
657 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
658 				     const char *name)
659 {
660 	struct tables *tables = container_of(dbe, struct tables, dbe);
661 	PyObject *t;
662 
663 	t = tuple_new(2);
664 
665 	tuple_set_s32(t, 0, branch_type);
666 	tuple_set_string(t, 1, name);
667 
668 	call_object(tables->branch_type_handler, t, "branch_type_table");
669 
670 	Py_DECREF(t);
671 
672 	return 0;
673 }
674 
675 static int python_export_sample(struct db_export *dbe,
676 				struct export_sample *es)
677 {
678 	struct tables *tables = container_of(dbe, struct tables, dbe);
679 	PyObject *t;
680 
681 	t = tuple_new(21);
682 
683 	tuple_set_u64(t, 0, es->db_id);
684 	tuple_set_u64(t, 1, es->evsel->db_id);
685 	tuple_set_u64(t, 2, es->al->machine->db_id);
686 	tuple_set_u64(t, 3, es->al->thread->db_id);
687 	tuple_set_u64(t, 4, es->comm_db_id);
688 	tuple_set_u64(t, 5, es->dso_db_id);
689 	tuple_set_u64(t, 6, es->sym_db_id);
690 	tuple_set_u64(t, 7, es->offset);
691 	tuple_set_u64(t, 8, es->sample->ip);
692 	tuple_set_u64(t, 9, es->sample->time);
693 	tuple_set_s32(t, 10, es->sample->cpu);
694 	tuple_set_u64(t, 11, es->addr_dso_db_id);
695 	tuple_set_u64(t, 12, es->addr_sym_db_id);
696 	tuple_set_u64(t, 13, es->addr_offset);
697 	tuple_set_u64(t, 14, es->sample->addr);
698 	tuple_set_u64(t, 15, es->sample->period);
699 	tuple_set_u64(t, 16, es->sample->weight);
700 	tuple_set_u64(t, 17, es->sample->transaction);
701 	tuple_set_u64(t, 18, es->sample->data_src);
702 	tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
703 	tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
704 
705 	call_object(tables->sample_handler, t, "sample_table");
706 
707 	Py_DECREF(t);
708 
709 	return 0;
710 }
711 
712 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
713 {
714 	struct tables *tables = container_of(dbe, struct tables, dbe);
715 	PyObject *t;
716 	u64 parent_db_id, sym_db_id;
717 
718 	parent_db_id = cp->parent ? cp->parent->db_id : 0;
719 	sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
720 
721 	t = tuple_new(4);
722 
723 	tuple_set_u64(t, 0, cp->db_id);
724 	tuple_set_u64(t, 1, parent_db_id);
725 	tuple_set_u64(t, 2, sym_db_id);
726 	tuple_set_u64(t, 3, cp->ip);
727 
728 	call_object(tables->call_path_handler, t, "call_path_table");
729 
730 	Py_DECREF(t);
731 
732 	return 0;
733 }
734 
735 static int python_export_call_return(struct db_export *dbe,
736 				     struct call_return *cr)
737 {
738 	struct tables *tables = container_of(dbe, struct tables, dbe);
739 	u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
740 	PyObject *t;
741 
742 	t = tuple_new(11);
743 
744 	tuple_set_u64(t, 0, cr->db_id);
745 	tuple_set_u64(t, 1, cr->thread->db_id);
746 	tuple_set_u64(t, 2, comm_db_id);
747 	tuple_set_u64(t, 3, cr->cp->db_id);
748 	tuple_set_u64(t, 4, cr->call_time);
749 	tuple_set_u64(t, 5, cr->return_time);
750 	tuple_set_u64(t, 6, cr->branch_count);
751 	tuple_set_u64(t, 7, cr->call_ref);
752 	tuple_set_u64(t, 8, cr->return_ref);
753 	tuple_set_u64(t, 9, cr->cp->parent->db_id);
754 	tuple_set_s32(t, 10, cr->flags);
755 
756 	call_object(tables->call_return_handler, t, "call_return_table");
757 
758 	Py_DECREF(t);
759 
760 	return 0;
761 }
762 
763 static int python_process_call_return(struct call_return *cr, void *data)
764 {
765 	struct db_export *dbe = data;
766 
767 	return db_export__call_return(dbe, cr);
768 }
769 
770 static void python_process_general_event(struct perf_sample *sample,
771 					 struct perf_evsel *evsel,
772 					 struct addr_location *al)
773 {
774 	PyObject *handler, *t, *dict, *callchain, *dict_sample;
775 	static char handler_name[64];
776 	unsigned n = 0;
777 
778 	/*
779 	 * Use the MAX_FIELDS to make the function expandable, though
780 	 * currently there is only one item for the tuple.
781 	 */
782 	t = PyTuple_New(MAX_FIELDS);
783 	if (!t)
784 		Py_FatalError("couldn't create Python tuple");
785 
786 	dict = PyDict_New();
787 	if (!dict)
788 		Py_FatalError("couldn't create Python dictionary");
789 
790 	dict_sample = PyDict_New();
791 	if (!dict_sample)
792 		Py_FatalError("couldn't create Python dictionary");
793 
794 	snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
795 
796 	handler = get_handler(handler_name);
797 	if (!handler)
798 		goto exit;
799 
800 	pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
801 	pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
802 			(const char *)&evsel->attr, sizeof(evsel->attr)));
803 
804 	pydict_set_item_string_decref(dict_sample, "pid",
805 			PyInt_FromLong(sample->pid));
806 	pydict_set_item_string_decref(dict_sample, "tid",
807 			PyInt_FromLong(sample->tid));
808 	pydict_set_item_string_decref(dict_sample, "cpu",
809 			PyInt_FromLong(sample->cpu));
810 	pydict_set_item_string_decref(dict_sample, "ip",
811 			PyLong_FromUnsignedLongLong(sample->ip));
812 	pydict_set_item_string_decref(dict_sample, "time",
813 			PyLong_FromUnsignedLongLong(sample->time));
814 	pydict_set_item_string_decref(dict_sample, "period",
815 			PyLong_FromUnsignedLongLong(sample->period));
816 	pydict_set_item_string_decref(dict, "sample", dict_sample);
817 
818 	pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
819 			(const char *)sample->raw_data, sample->raw_size));
820 	pydict_set_item_string_decref(dict, "comm",
821 			PyString_FromString(thread__comm_str(al->thread)));
822 	if (al->map) {
823 		pydict_set_item_string_decref(dict, "dso",
824 			PyString_FromString(al->map->dso->name));
825 	}
826 	if (al->sym) {
827 		pydict_set_item_string_decref(dict, "symbol",
828 			PyString_FromString(al->sym->name));
829 	}
830 
831 	/* ip unwinding */
832 	callchain = python_process_callchain(sample, evsel, al);
833 	pydict_set_item_string_decref(dict, "callchain", callchain);
834 
835 	PyTuple_SetItem(t, n++, dict);
836 	if (_PyTuple_Resize(&t, n) == -1)
837 		Py_FatalError("error resizing Python tuple");
838 
839 	call_object(handler, t, handler_name);
840 exit:
841 	Py_DECREF(dict);
842 	Py_DECREF(t);
843 }
844 
845 static void python_process_event(union perf_event *event,
846 				 struct perf_sample *sample,
847 				 struct perf_evsel *evsel,
848 				 struct addr_location *al)
849 {
850 	struct tables *tables = &tables_global;
851 
852 	switch (evsel->attr.type) {
853 	case PERF_TYPE_TRACEPOINT:
854 		python_process_tracepoint(sample, evsel, al);
855 		break;
856 	/* Reserve for future process_hw/sw/raw APIs */
857 	default:
858 		if (tables->db_export_mode)
859 			db_export__sample(&tables->dbe, event, sample, evsel, al);
860 		else
861 			python_process_general_event(sample, evsel, al);
862 	}
863 }
864 
865 static void get_handler_name(char *str, size_t size,
866 			     struct perf_evsel *evsel)
867 {
868 	char *p = str;
869 
870 	scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
871 
872 	while ((p = strchr(p, ':'))) {
873 		*p = '_';
874 		p++;
875 	}
876 }
877 
878 static void
879 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
880 	     struct perf_counts_values *count)
881 {
882 	PyObject *handler, *t;
883 	static char handler_name[256];
884 	int n = 0;
885 
886 	t = PyTuple_New(MAX_FIELDS);
887 	if (!t)
888 		Py_FatalError("couldn't create Python tuple");
889 
890 	get_handler_name(handler_name, sizeof(handler_name),
891 			 counter);
892 
893 	handler = get_handler(handler_name);
894 	if (!handler) {
895 		pr_debug("can't find python handler %s\n", handler_name);
896 		return;
897 	}
898 
899 	PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
900 	PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
901 
902 	tuple_set_u64(t, n++, tstamp);
903 	tuple_set_u64(t, n++, count->val);
904 	tuple_set_u64(t, n++, count->ena);
905 	tuple_set_u64(t, n++, count->run);
906 
907 	if (_PyTuple_Resize(&t, n) == -1)
908 		Py_FatalError("error resizing Python tuple");
909 
910 	call_object(handler, t, handler_name);
911 
912 	Py_DECREF(t);
913 }
914 
915 static void python_process_stat(struct perf_stat_config *config,
916 				struct perf_evsel *counter, u64 tstamp)
917 {
918 	struct thread_map *threads = counter->threads;
919 	struct cpu_map *cpus = counter->cpus;
920 	int cpu, thread;
921 
922 	if (config->aggr_mode == AGGR_GLOBAL) {
923 		process_stat(counter, -1, -1, tstamp,
924 			     &counter->counts->aggr);
925 		return;
926 	}
927 
928 	for (thread = 0; thread < threads->nr; thread++) {
929 		for (cpu = 0; cpu < cpus->nr; cpu++) {
930 			process_stat(counter, cpus->map[cpu],
931 				     thread_map__pid(threads, thread), tstamp,
932 				     perf_counts(counter->counts, cpu, thread));
933 		}
934 	}
935 }
936 
937 static void python_process_stat_interval(u64 tstamp)
938 {
939 	PyObject *handler, *t;
940 	static const char handler_name[] = "stat__interval";
941 	int n = 0;
942 
943 	t = PyTuple_New(MAX_FIELDS);
944 	if (!t)
945 		Py_FatalError("couldn't create Python tuple");
946 
947 	handler = get_handler(handler_name);
948 	if (!handler) {
949 		pr_debug("can't find python handler %s\n", handler_name);
950 		return;
951 	}
952 
953 	tuple_set_u64(t, n++, tstamp);
954 
955 	if (_PyTuple_Resize(&t, n) == -1)
956 		Py_FatalError("error resizing Python tuple");
957 
958 	call_object(handler, t, handler_name);
959 
960 	Py_DECREF(t);
961 }
962 
963 static int run_start_sub(void)
964 {
965 	main_module = PyImport_AddModule("__main__");
966 	if (main_module == NULL)
967 		return -1;
968 	Py_INCREF(main_module);
969 
970 	main_dict = PyModule_GetDict(main_module);
971 	if (main_dict == NULL)
972 		goto error;
973 	Py_INCREF(main_dict);
974 
975 	try_call_object("trace_begin", NULL);
976 
977 	return 0;
978 
979 error:
980 	Py_XDECREF(main_dict);
981 	Py_XDECREF(main_module);
982 	return -1;
983 }
984 
985 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do {		\
986 	tables->handler_name = get_handler(#table_name);		\
987 	if (tables->handler_name)					\
988 		tables->dbe.export_ ## name = python_export_ ## name;	\
989 } while (0)
990 
991 #define SET_TABLE_HANDLER(name) \
992 	SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
993 
994 static void set_table_handlers(struct tables *tables)
995 {
996 	const char *perf_db_export_mode = "perf_db_export_mode";
997 	const char *perf_db_export_calls = "perf_db_export_calls";
998 	PyObject *db_export_mode, *db_export_calls;
999 	bool export_calls = false;
1000 	int ret;
1001 
1002 	memset(tables, 0, sizeof(struct tables));
1003 	if (db_export__init(&tables->dbe))
1004 		Py_FatalError("failed to initialize export");
1005 
1006 	db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1007 	if (!db_export_mode)
1008 		return;
1009 
1010 	ret = PyObject_IsTrue(db_export_mode);
1011 	if (ret == -1)
1012 		handler_call_die(perf_db_export_mode);
1013 	if (!ret)
1014 		return;
1015 
1016 	tables->dbe.crp = NULL;
1017 	db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1018 	if (db_export_calls) {
1019 		ret = PyObject_IsTrue(db_export_calls);
1020 		if (ret == -1)
1021 			handler_call_die(perf_db_export_calls);
1022 		export_calls = !!ret;
1023 	}
1024 
1025 	if (export_calls) {
1026 		tables->dbe.crp =
1027 			call_return_processor__new(python_process_call_return,
1028 						   &tables->dbe);
1029 		if (!tables->dbe.crp)
1030 			Py_FatalError("failed to create calls processor");
1031 	}
1032 
1033 	tables->db_export_mode = true;
1034 	/*
1035 	 * Reserve per symbol space for symbol->db_id via symbol__priv()
1036 	 */
1037 	symbol_conf.priv_size = sizeof(u64);
1038 
1039 	SET_TABLE_HANDLER(evsel);
1040 	SET_TABLE_HANDLER(machine);
1041 	SET_TABLE_HANDLER(thread);
1042 	SET_TABLE_HANDLER(comm);
1043 	SET_TABLE_HANDLER(comm_thread);
1044 	SET_TABLE_HANDLER(dso);
1045 	SET_TABLE_HANDLER(symbol);
1046 	SET_TABLE_HANDLER(branch_type);
1047 	SET_TABLE_HANDLER(sample);
1048 	SET_TABLE_HANDLER(call_path);
1049 	SET_TABLE_HANDLER(call_return);
1050 }
1051 
1052 /*
1053  * Start trace script
1054  */
1055 static int python_start_script(const char *script, int argc, const char **argv)
1056 {
1057 	struct tables *tables = &tables_global;
1058 	const char **command_line;
1059 	char buf[PATH_MAX];
1060 	int i, err = 0;
1061 	FILE *fp;
1062 
1063 	command_line = malloc((argc + 1) * sizeof(const char *));
1064 	command_line[0] = script;
1065 	for (i = 1; i < argc + 1; i++)
1066 		command_line[i] = argv[i - 1];
1067 
1068 	Py_Initialize();
1069 
1070 	initperf_trace_context();
1071 
1072 	PySys_SetArgv(argc + 1, (char **)command_line);
1073 
1074 	fp = fopen(script, "r");
1075 	if (!fp) {
1076 		sprintf(buf, "Can't open python script \"%s\"", script);
1077 		perror(buf);
1078 		err = -1;
1079 		goto error;
1080 	}
1081 
1082 	err = PyRun_SimpleFile(fp, script);
1083 	if (err) {
1084 		fprintf(stderr, "Error running python script %s\n", script);
1085 		goto error;
1086 	}
1087 
1088 	err = run_start_sub();
1089 	if (err) {
1090 		fprintf(stderr, "Error starting python script %s\n", script);
1091 		goto error;
1092 	}
1093 
1094 	free(command_line);
1095 
1096 	set_table_handlers(tables);
1097 
1098 	if (tables->db_export_mode) {
1099 		err = db_export__branch_types(&tables->dbe);
1100 		if (err)
1101 			goto error;
1102 	}
1103 
1104 	return err;
1105 error:
1106 	Py_Finalize();
1107 	free(command_line);
1108 
1109 	return err;
1110 }
1111 
1112 static int python_flush_script(void)
1113 {
1114 	struct tables *tables = &tables_global;
1115 
1116 	return db_export__flush(&tables->dbe);
1117 }
1118 
1119 /*
1120  * Stop trace script
1121  */
1122 static int python_stop_script(void)
1123 {
1124 	struct tables *tables = &tables_global;
1125 
1126 	try_call_object("trace_end", NULL);
1127 
1128 	db_export__exit(&tables->dbe);
1129 
1130 	Py_XDECREF(main_dict);
1131 	Py_XDECREF(main_module);
1132 	Py_Finalize();
1133 
1134 	return 0;
1135 }
1136 
1137 static int python_generate_script(struct pevent *pevent, const char *outfile)
1138 {
1139 	struct event_format *event = NULL;
1140 	struct format_field *f;
1141 	char fname[PATH_MAX];
1142 	int not_first, count;
1143 	FILE *ofp;
1144 
1145 	sprintf(fname, "%s.py", outfile);
1146 	ofp = fopen(fname, "w");
1147 	if (ofp == NULL) {
1148 		fprintf(stderr, "couldn't open %s\n", fname);
1149 		return -1;
1150 	}
1151 	fprintf(ofp, "# perf script event handlers, "
1152 		"generated by perf script -g python\n");
1153 
1154 	fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1155 		" License version 2\n\n");
1156 
1157 	fprintf(ofp, "# The common_* event handler fields are the most useful "
1158 		"fields common to\n");
1159 
1160 	fprintf(ofp, "# all events.  They don't necessarily correspond to "
1161 		"the 'common_*' fields\n");
1162 
1163 	fprintf(ofp, "# in the format files.  Those fields not available as "
1164 		"handler params can\n");
1165 
1166 	fprintf(ofp, "# be retrieved using Python functions of the form "
1167 		"common_*(context).\n");
1168 
1169 	fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1170 		"of available functions.\n\n");
1171 
1172 	fprintf(ofp, "import os\n");
1173 	fprintf(ofp, "import sys\n\n");
1174 
1175 	fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1176 	fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1177 	fprintf(ofp, "\nfrom perf_trace_context import *\n");
1178 	fprintf(ofp, "from Core import *\n\n\n");
1179 
1180 	fprintf(ofp, "def trace_begin():\n");
1181 	fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1182 
1183 	fprintf(ofp, "def trace_end():\n");
1184 	fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1185 
1186 	while ((event = trace_find_next_event(pevent, event))) {
1187 		fprintf(ofp, "def %s__%s(", event->system, event->name);
1188 		fprintf(ofp, "event_name, ");
1189 		fprintf(ofp, "context, ");
1190 		fprintf(ofp, "common_cpu,\n");
1191 		fprintf(ofp, "\tcommon_secs, ");
1192 		fprintf(ofp, "common_nsecs, ");
1193 		fprintf(ofp, "common_pid, ");
1194 		fprintf(ofp, "common_comm,\n\t");
1195 		fprintf(ofp, "common_callchain, ");
1196 
1197 		not_first = 0;
1198 		count = 0;
1199 
1200 		for (f = event->format.fields; f; f = f->next) {
1201 			if (not_first++)
1202 				fprintf(ofp, ", ");
1203 			if (++count % 5 == 0)
1204 				fprintf(ofp, "\n\t");
1205 
1206 			fprintf(ofp, "%s", f->name);
1207 		}
1208 		fprintf(ofp, "):\n");
1209 
1210 		fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1211 			"common_secs, common_nsecs,\n\t\t\t"
1212 			"common_pid, common_comm)\n\n");
1213 
1214 		fprintf(ofp, "\t\tprint \"");
1215 
1216 		not_first = 0;
1217 		count = 0;
1218 
1219 		for (f = event->format.fields; f; f = f->next) {
1220 			if (not_first++)
1221 				fprintf(ofp, ", ");
1222 			if (count && count % 3 == 0) {
1223 				fprintf(ofp, "\" \\\n\t\t\"");
1224 			}
1225 			count++;
1226 
1227 			fprintf(ofp, "%s=", f->name);
1228 			if (f->flags & FIELD_IS_STRING ||
1229 			    f->flags & FIELD_IS_FLAG ||
1230 			    f->flags & FIELD_IS_ARRAY ||
1231 			    f->flags & FIELD_IS_SYMBOLIC)
1232 				fprintf(ofp, "%%s");
1233 			else if (f->flags & FIELD_IS_SIGNED)
1234 				fprintf(ofp, "%%d");
1235 			else
1236 				fprintf(ofp, "%%u");
1237 		}
1238 
1239 		fprintf(ofp, "\" %% \\\n\t\t(");
1240 
1241 		not_first = 0;
1242 		count = 0;
1243 
1244 		for (f = event->format.fields; f; f = f->next) {
1245 			if (not_first++)
1246 				fprintf(ofp, ", ");
1247 
1248 			if (++count % 5 == 0)
1249 				fprintf(ofp, "\n\t\t");
1250 
1251 			if (f->flags & FIELD_IS_FLAG) {
1252 				if ((count - 1) % 5 != 0) {
1253 					fprintf(ofp, "\n\t\t");
1254 					count = 4;
1255 				}
1256 				fprintf(ofp, "flag_str(\"");
1257 				fprintf(ofp, "%s__%s\", ", event->system,
1258 					event->name);
1259 				fprintf(ofp, "\"%s\", %s)", f->name,
1260 					f->name);
1261 			} else if (f->flags & FIELD_IS_SYMBOLIC) {
1262 				if ((count - 1) % 5 != 0) {
1263 					fprintf(ofp, "\n\t\t");
1264 					count = 4;
1265 				}
1266 				fprintf(ofp, "symbol_str(\"");
1267 				fprintf(ofp, "%s__%s\", ", event->system,
1268 					event->name);
1269 				fprintf(ofp, "\"%s\", %s)", f->name,
1270 					f->name);
1271 			} else
1272 				fprintf(ofp, "%s", f->name);
1273 		}
1274 
1275 		fprintf(ofp, ")\n\n");
1276 
1277 		fprintf(ofp, "\t\tfor node in common_callchain:");
1278 		fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1279 		fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1280 		fprintf(ofp, "\n\t\t\telse:");
1281 		fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1282 		fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1283 
1284 	}
1285 
1286 	fprintf(ofp, "def trace_unhandled(event_name, context, "
1287 		"event_fields_dict):\n");
1288 
1289 	fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1290 		"for k,v in sorted(event_fields_dict.items())])\n\n");
1291 
1292 	fprintf(ofp, "def print_header("
1293 		"event_name, cpu, secs, nsecs, pid, comm):\n"
1294 		"\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1295 		"(event_name, cpu, secs, nsecs, pid, comm),\n");
1296 
1297 	fclose(ofp);
1298 
1299 	fprintf(stderr, "generated Python script: %s\n", fname);
1300 
1301 	return 0;
1302 }
1303 
1304 struct scripting_ops python_scripting_ops = {
1305 	.name			= "Python",
1306 	.start_script		= python_start_script,
1307 	.flush_script		= python_flush_script,
1308 	.stop_script		= python_stop_script,
1309 	.process_event		= python_process_event,
1310 	.process_stat		= python_process_stat,
1311 	.process_stat_interval	= python_process_stat_interval,
1312 	.generate_script	= python_generate_script,
1313 };
1314