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